#29123: Changing an IntegerField to a ForeignKey generates incorrectly ordered
migration operations if the field is in unique_together
----------------------------+------------------------------------
     Reporter:  Ed Morley   |                    Owner:  Jeff
         Type:  Bug         |                   Status:  assigned
    Component:  Migrations  |                  Version:  master
     Severity:  Normal      |               Resolution:
     Keywords:              |             Triage Stage:  Accepted
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+------------------------------------

Comment (by Jeff):

 It looks like the example migrations provided are exposing two separate
 issues.

 1) improperly ordered AddField/RemoveField/AlterUniqueTogether. Below is
 the initial migration

 Original Models:
 {{{
     class Foo(models.Model):
         pass


     class Bar(models.Model):
         name = models.CharField(max_length=50)
         foo_id = models.PositiveIntegerField()

         class Meta:
             unique_together = ('name', 'foo_id')
 }}}

 Updated Models:
 {{{
     class Foo(models.Model):
         pass


     class Bar(models.Model):
         name = models.CharField(max_length=
         baz = models.ForeignKey(Foo, models.CASCADE, null=True)

         class Meta:
             unique_together = ('name', 'baz')
 }}}

 This produces the problematic migration:
 {{{
     class Migration(migrations.Migration):

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

         operations = [
             migrations.AddField(
                 model_name='bar',
                 name='baz',
                 field=models.ForeignKey(null=True,
 on_delete=django.db.models.deletion.CASCADE, to='temp.Foo'),
             ),
             migrations.RemoveField(
                 model_name='bar',
                 name='foo_id',
             ),
             migrations.AlterUniqueTogether(
                 name='bar',
                 unique_together={('name', 'baz')},
             ),
         ]
 }}}

 Instead of this functional migration:
 {{{
     class Migration(migrations.Migration):

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

         operations = [
             migrations.AddField(
                 model_name='bar',
                 name='baz',
                 field=models.ForeignKey(null=True,
 on_delete=django.db.models.deletion.CASCADE, to='temp.Foo'),
             ),
             migrations.AlterUniqueTogether(
                 name='bar',
                 unique_together={('name', 'baz')},
             ),
             migrations.RemoveField(
                 model_name='bar',
                 name='foo_id',
             ),
         ]
 }}}

 and 2) problematic behavior around the clashing of names like `foo` with
 `foo_id`.

 I believe the given examples somewhat conflated these two separate issues,
 while I believe 1) is a bug and should be fixed, the I believe 2) is
 working as intended.
 Please advise if I am incorrect on this assumption.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29123#comment:5>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.e0a16fdd40b1aae216c47195ed434370%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to