#34128 closed Uncategorized (invalid)

django re-using the name of a squashed migration leads to CircularDependencyError

Reported by: Johannes 'fish' Ziemke Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Johannes 'fish' Ziemke)

Hi,

I've squashed two migrations, creating this file:

# Generated by Django 3.2 on 2022-10-30 14:15

from django.db import migrations, models


class Migration(migrations.Migration):

    replaces = [('api', '0005_achievement_difficulty'), ('api', '0006_alter_achievement_difficulty')]

    dependencies = [
        ('api', '0004_achievement_squashed_0005_alter_achievement_submission'),
    ]

    operations = [
        migrations.AddField(
            model_name='achievement',
            name='difficulty',
            field=models.SmallIntegerField(default=0),
        ),
    ]

After rolling out the change, I've deleted 0005_achievement_difficulty and 0006_alter_achievement_difficulty.

Now I've did some more changes to the model, causing django to create the following 0006_alter_achievement_difficulty:

# Generated by Django 3.2 on 2022-10-31 12:59

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0005_achievement_difficulty_squashed_0006_alter_achievement_difficulty'),
    ]

    operations = [
        migrations.AlterField(
            model_name='achievement',
            name='difficulty',
            field=models.SmallIntegerField(),
        ),
    ]

Which leads to the following error:

  File "/usr/local/lib/python3.10/site-packages/django/db/migrations/graph.py", line 274, in ensure_not_cyclic
    raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: api.0005_achievement_difficulty_squashed_0006_alter_achievement_difficulty

Renaming the migration file to 0006_alter_achievement_difficulty_new.py fixed it.

While I wouldn't rule out a mistake on my side, it seems the way django creates the migration name can lead to new migrations being named like squashed and removed ones, causing the dependency check to wrongfully assume the squashed migration would depend on it.

Change History (2)

comment:1 by Johannes 'fish' Ziemke, 18 months ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 18 months ago

Resolution: invalid
Status: newclosed

Thanks for this ticket, however transition of the squashed migration to a normal migration is more complicated and requires removing the replaces attribute, see docs:

You must then transition the squashed migration to a normal migration

  • Deleting all the migration files it replaces.
  • Updating all migrations that depend on the deleted migrations to depend on the squashed migration instead
  • Removing the replaces attribute in the Migration class of the squashed migration (this is how Django tells that it is a squashed migration).
Note: See TracTickets for help on using tickets.
Back to Top