[Django] #28916: Changing the type of a ForeignKey and changing unique_together creates migrations in the wrong order, causing migrations to fail

2 views
Skip to first unread message

Django

unread,
Dec 12, 2017, 10:50:38 AM12/12/17
to django-...@googlegroups.com
#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: Uncategorized | Version: 2.0
Severity: Normal | Keywords: migrations
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+----------------------------
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`.

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

Django

unread,
Dec 12, 2017, 10:52:17 AM12/12/17
to django-...@googlegroups.com
#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
----------------------------+--------------------------------------
Changes (by fredley):

* component: Uncategorized => Migrations


--
Ticket URL: <https://code.djangoproject.com/ticket/28916#comment:1>

Django

unread,
Dec 12, 2017, 10:54:09 AM12/12/17
to django-...@googlegroups.com
#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:
>

> models.py after:
>

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

unread,
Dec 18, 2017, 10:35:41 AM12/18/17
to django-...@googlegroups.com
#28916: Changing the type of a ForeignKey and changing unique_together creates
migrations in the wrong order, causing migrations to fail
----------------------------+--------------------------------------
Reporter: fredley | Owner: Junji Wei
Type: Bug | Status: assigned

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
----------------------------+--------------------------------------
Changes (by Junji Wei):

* owner: nobody => Junji Wei
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28916#comment:3>

Django

unread,
Dec 18, 2017, 10:40:58 AM12/18/17
to django-...@googlegroups.com
#28916: Changing the type of a ForeignKey and changing unique_together creates
migrations in the wrong order, causing migrations to fail
----------------------------+--------------------------------------
Reporter: fredley | Owner: (none)
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
----------------------------+--------------------------------------
Changes (by Junji Wei):

* status: assigned => new
* owner: Junji Wei => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/28916#comment:4>

Django

unread,
Dec 18, 2017, 10:46:49 AM12/18/17
to django-...@googlegroups.com
#28916: Changing the type of a ForeignKey and changing unique_together creates
migrations in the wrong order, causing migrations to fail
----------------------------+--------------------------------------
Reporter: fredley | Owner: (none)
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: duplicate

Keywords: migrations | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by Ramiro Morales):

* status: new => closed
* resolution: => duplicate


Comment:

I think this is a duplicate of #28862.

--
Ticket URL: <https://code.djangoproject.com/ticket/28916#comment:5>

Reply all
Reply to author
Forward
0 new messages