Opened 7 years ago

Last modified 7 years ago

#28916 closed Bug

Changing the type of a ForeignKey and changing unique_together creates migrations in the wrong order, causing migrations to fail — at Initial Version

Reported by: fredley Owned by: nobody
Component: Migrations Version: 2.0
Severity: Normal Keywords: migrations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

models.py before:

class MyModel(models.Model):
    fka = models.ForeignKey(SomethingA, ...)
    date = models.DateField()

    class Meta:
        unique_together = ('fka', 'date')

models.py after:

class MyModel(models.Model):
    fkb = models.ForeignKey(SomethingB, ...)
    date = models.DateField()

    class Meta:
        unique_together = ('fkb', 'date')

This can result in an automatically created migration (using manage.py migrate) that looks like this:

operations = [
    migrations.AddField(
        model_name='mymodel',
        name='fkb',
        field=models.ForeignKey(... to='myapp.somethingb'),
    ),
    migrations.RemoveField(
        model_name='mymodel',
        name='fka',
    ),
    migrations.AlterUniqueTogether(
        name='mymodel',
        unique_together={('fkb', 'date')},
    ),
]

This migration fails, because AlterUniqueTogether needs to come before RemoveField, as it references fka.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top