#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.0c01df8186f6f1dadfa9d0bf2ed894c2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to