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 Tim Graham, 10 years ago

Possibly related/duplicate of #22944.

comment:2 by Andrew Godwin, 10 years ago

Owner: changed from nobody to Andrew Godwin
Status: newassigned

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.

Last edited 10 years ago by Andrew Godwin (previous) (diff)

comment:3 by Andrew Godwin <andrew@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 008bff92b7028673cdab872a6733770606efce83:

Fixed #22970: Incorrect dependencies for existing migrated apps

comment:4 by Andrew Godwin <andrew@…>, 10 years ago

In aba75e73db6a0baca1b721698ca24f026bb4a745:

[1.7.x] Fixed #22970: Incorrect dependencies for existing migrated apps

comment:5 by Jeroen Dekkers, 10 years ago

Resolution: fixed
Status: closednew

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 Tim Graham, 10 years ago

Resolution: fixed
Status: newclosed

There was a typo and it should be __latest__; see #23008.

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