#22970 closed Bug (fixed)

Migration dependencies not correctly calculated

Reported by: dekkers Owned by: andrewgodwin
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 Changed 14 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Possibly related/duplicate of #22944.

comment:2 Changed 14 months ago by andrewgodwin

  • Owner changed from nobody to andrewgodwin
  • Status changed from new to 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.

Last edited 14 months ago by andrewgodwin (previous) (diff)

comment:3 Changed 14 months ago by Andrew Godwin <andrew@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 008bff92b7028673cdab872a6733770606efce83:

Fixed #22970: Incorrect dependencies for existing migrated apps

comment:4 Changed 14 months ago by Andrew Godwin <andrew@…>

In aba75e73db6a0baca1b721698ca24f026bb4a745:

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

comment:5 Changed 14 months ago by dekkers

  • Resolution fixed deleted
  • Status changed from closed to 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 Changed 14 months ago by timo

  • Resolution set to fixed
  • Status changed from new to closed

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

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