Django `squashmigrations` leaving lots of RemoveField

I am trying to use Django manage.py squashmigrations to reduce the number of migration files and migration operations, after years of accumulation.

Django should supposedly optimize the migrations and it does so to some extent. Other cases, that should be obvious optimization are missed though, for example below where a simple AddField+RemoveField field is not. Only the AddField gets inlined into the CreateModel, but then the RemoveField still remains, instead of completely leaving out the field from CreateModel.

Ideally, there shouldn't be a single RemoveField left after a squash, if I'm not mistaken.

Migration 1: Add the model

class Migration(migrations.Migration):
    dependencies = [("data", "0104")]

    operations = [
        migrations.CreateModel(
                    name="MyModel",
                    fields=[... ]
        )
    ]

Migration 2: Add a field to the model

class Migration(migrations.Migration):
    dependencies = [("data", "0107")]

    operations = [
        migrations.AddField(
            model_name="mymodel",
            name="process_name",
            field=models.CharField(default=None, max_length=300, null=True),
        ),
    ]

Migration 3: Remove the same field from the model

class Migration(migrations.Migration):
    dependencies = [("data", "0121")]

    operations = [migrations.RemoveField(model_name="mymodel", name="process_name")]

Resulting squashed migration:

class Migration(migrations.Migration):
    replaces = [...]

    initial = True

    operations = [
        migrations.CreateModel(
            name="MyModel",
            fields=[
                (
                    "process_name",                  # Should be possible to leave out ???
                    models.CharField(default=None, max_length=300, null=True),
                ),
            ],
        ),
        migrations.RemoveField(model_name="mymodel", name="process_name"),           # ???
    ]

Is this expected or what could cause this?

I am not using any advanced migration features such as RunSql or RunPython. There are no other tables or migrations that depend to the process_name field.

Are there any way to give hints to squashmigrations? Can i run squash again to optimize further?

(Django version 5.1)

Вернуться на верх