Opened 10 years ago
Closed 10 years ago
#22970 closed Bug (fixed)
Migration dependencies not correctly calculated
Reported by: | Jeroen Dekkers | Owned by: | Andrew Godwin |
---|---|---|---|
Component: | Migrations | Version: | 1.7-rc-1 |
Severity: | Release blocker | 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
When working with the new migrations I hit a bug that dependencies between migrations aren't correctly calculated. Migrations that add a ForeignKey to a model in another app that was created in the second or later migrations of that other app have a depenency on the first migration instead of the later migration. Steps to reproduce:
- Start a new project and create two apps myapp1 and myapp2
- Add a model named Model1 to myapp1
- Run makemigrations, resulting in 0001_initial.py that creates Model1
- Add a model named Model2 to myapp1
- Run makemigrations, resulting in 0002_model2.py that creates Model2
- Add a model named Model3 with a ForeignKey to 'myapp1.Model2' to myapp2
- Run makemigrations, resulting in 0001_initial.py that creates Model3
The 0001_initial.py of myapp2 will then have ('myapp1', '__first__')
as dependency while Model2 is created in the second migration.
Change History (6)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I suspect what's needed here is for the migration to depend on __last__
, which we definitely used to do and I backed out for some reason.
comment:3 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:5 by , 10 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
Your fix didn't work, when I ran migrate I got the following error:
KeyError: u"Dependency references nonexistent parent node (u'myapp1', u'__last__')"
It looks like __last__
isn't implemented, there isn't any other mention of it in the source. I took a look at the code and fixed it in a different way: get the name of the app's last migration from the graph and use that. I did some quick tests and everything seems to work fine.
Pull request: https://github.com/django/django/pull/2917
comment:6 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
There was a typo and it should be __latest__
; see #23008.
Possibly related/duplicate of #22944.