#28916: Changing the type of a ForeignKey and changing unique_together creates
migrations in the wrong order, causing migrations to fail
----------------------------+--------------------------------------
     Reporter:  fredley     |                    Owner:  nobody
         Type:  Bug         |                   Status:  new
    Component:  Migrations  |                  Version:  2.0
     Severity:  Normal      |               Resolution:
     Keywords:  migrations  |             Triage Stage:  Unreviewed
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+--------------------------------------
Description changed by fredley:

Old 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`.

New 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
 makemigrations`) 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`. This is hard to debug, and can only
 be fixed by manually reordering the migration operations.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28916#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.733eeb0d2e8892bc68ac765d2ad2183c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to