Custom SQL In Django Migration

In my project, one use case came with custom SQL migration.

Step1:

Create the empty migration

python manage.py makemigrations <project_name> --empty -n <relevant message>
# python manage.py makemigrations app --empty -n creating_users

Step 2:

Open created migration.py and add your SQL script

file: 0002_creating_users.py

from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ('permissions', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL(

            sql=[
                (
                    "INSERT INTO auth_user (username, first_name) VALUES (%s, %s);",
                    ['test_u','test_first_name']
                ),
                (
                    "INSERT INTO empoyees (username, first_name) VALUES (%s, %s);",
                    ['test_e', 'test_first_name']
                )

            ]
        )
    ]

Step3:

Apply migration

python manage.py migrate