How would you create a 'manual' django migration?

You can create a manual migration by running the command:

python manage.py makemigrations app_name --name migration_name --empty

Where app_name corresponds to the app within your project you want to add the migration. Remember Django manages both Project and Apps (A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.) And --empty flag is to create a migration file where you will have to add your manual migrations.

For example, on a project where you have an app named api that only has one migration file 0001_initial.py running:

python manage.py makemigrations api --name migration_example --empty

will create a file named 0002_migration_example.py under the directory api/migrations/ that will look like:

# Generated by Django 2.2.10 on 2020-05-26 20:37

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0001_initial'),
    ]

    operations = [
    ]

And you should add migrations.RunSQL('some sql'). inside operations brackets, like:

    operations = [
      migrations.RunSQL('some sql')
    ]

You can learn how to make migrations by investigating the automatically generated migrations, for example:


class Migration(migrations.Migration):

    dependencies = [
        ('app_details', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            ...,
        ),
        migrations.CreateModel(
            ...,
        ),
        migrations.RenameModel(
            ...,
        ),
        migrations.RenameField(
            ...,
        ),
        migrations.RemoveModel(
            ...,
        ),
        # and so on
    ]


Create a manual migration file

use this command in the terminal: python manage.py makemigrations --empty. Then add what you want in it.

notice: you have to compose between the "models.py" and the manual migrations.

Tags:

Django