[Django] #23100: Applying migrations fails when adding a ForeignKey to swappable user model after initial migration

4 views
Skip to first unread message

Django

unread,
Jul 25, 2014, 3:21:24 PM7/25/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
----------------------------+----------------------
Reporter: mattias | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-rc-1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+----------------------
./manage.py migrate fails with `ValueError: Lookup failed for model
referenced by field foo.A.owner: accounts.EmailUser` or similar when a
ForeignKey to a swapped out user model is added '''after the initial
migration'''.

Note that both making the migration and applying it work if the ForeignKey
to the swappable user model is added in the very first initial migration,
but not if it is added later.

I've tested this on both the 1.7 RC1 version, and the latest github
checkout of branch "stable/1.7.x".

Steps to reproduce:
1. Create a swappable user model, add it to settings.py, create and run
migrations.
2. Create a new model in a new app. Do not give it a reference to the user
model.
3. Add a ForeignKey from the new model to the custom user model.
4. Run makemigrations. This works.
5. Run migrate. This fails.

Workaround:
1. Add `migrations.swappable_dependency(settings.AUTH_USER_MODEL),` to the
list of dependencies of the failing migration by hand.

Initial investigation:
Swappable user ForeignKeys are referenced as `('owner',
models.ForeignKey(to=settings.AUTH_USER_MODEL)),` in the migration
scripts.

It seems that if the ForeignKey is added in the initial migration of a new
app, then dependencies are correctly added:
{{{
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
}}}

But if the ForeignKey is added in a later migration, then the initial
migration's dependencies are empty, and the second migration's only
dependency is:
{{{
dependencies = [
('foo', '0001_initial'),
]
}}}

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

Django

unread,
Jul 25, 2014, 3:26:06 PM7/25/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
----------------------------+--------------------------------------

Reporter: mattias | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by mattias):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Sorry, I missed a step in the reproduction instructions. They are:

1. Create a swappable user model, add it to settings.py, create and run
migrations.
2. Create a new model in a new app. Do not give it a reference to the user
model.

3. Run makemigrations and migrate. Both work.
4. Add a ForeignKey from the new model to the custom user model.
5. Run makemigrations. This works.
6. Run migrate. This fails.

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

Django

unread,
Jul 28, 2014, 7:11:59 AM7/28/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
----------------------------+--------------------------------------

Reporter: mattias | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-rc-2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by Mattias Linnap <mattias@…>):

* version: 1.7-rc-1 => 1.7-rc-2


Comment:

Also tested on 1.7 RC2.

--
Ticket URL: <https://code.djangoproject.com/ticket/23100#comment:2>

Django

unread,
Jul 28, 2014, 1:09:42 PM7/28/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
----------------------------+--------------------------------------

Reporter: mattias | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-rc-2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------

Comment (by andrewgodwin):

I suspect this is a duplicate of #22944.

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

Django

unread,
Jul 28, 2014, 1:16:28 PM7/28/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
---------------------------------+----------------------------------------
Reporter: mattias | Owner: andrewgodwin
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7-rc-2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------
Changes (by andrewgodwin):

* status: new => assigned
* owner: nobody => andrewgodwin
* severity: Normal => Release blocker


Comment:

Closed #22944 in favour of this, promoting this to release blocker.

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

Django

unread,
Jul 28, 2014, 1:33:45 PM7/28/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
---------------------------------+----------------------------------------
Reporter: mattias | Owner: andrewgodwin
Type: Bug | Status: closed
Component: Migrations | Version: 1.7-rc-2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------
Changes (by Andrew Godwin <andrew@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"cb60d22bd94738174440f8d60a04c3d9c38636c9"]:
{{{
#!CommitTicketReference repository=""
revision="cb60d22bd94738174440f8d60a04c3d9c38636c9"
Fixed #23100: Individual FK creation missing dependencies
}}}

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

Django

unread,
Jul 28, 2014, 1:34:04 PM7/28/14
to django-...@googlegroups.com
#23100: Applying migrations fails when adding a ForeignKey to swappable user model
after initial migration
---------------------------------+----------------------------------------
Reporter: mattias | Owner: andrewgodwin
Type: Bug | Status: closed
Component: Migrations | Version: 1.7-rc-2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------

Comment (by Andrew Godwin <andrew@…>):

In [changeset:"d84b881600788e9a9d3c2ad7d3fe4a03d1880086"]:
{{{
#!CommitTicketReference repository=""
revision="d84b881600788e9a9d3c2ad7d3fe4a03d1880086"
[1.7.x] Fixed #23100: Individual FK creation missing dependencies
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23100#comment:6>

Reply all
Reply to author
Forward
0 new messages