[Django] #22848: Admin initial migration run before app containing AUTH_USER_MODEL

38 views
Skip to first unread message

Django

unread,
Jun 16, 2014, 8:04:39 AM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration run before app containing AUTH_USER_MODEL
---------------------------------+--------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
Defining a custom user model (in app 'testapp' and with
settings.AUTH_USER_MODEL = 'testapp.CustomUser'):

{{{
from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
foo = models.CharField(max_length=255)
}}}

And trying to make an initial migration, results in an error:

{{{
$ ./manage.py makemigrations
Traceback (most recent call last):
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/db/migrations/loader.py", line 155, in check_key
return list(self.graph.root_nodes(key[0]))[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/core/management/__init__.py", line 385, in
execute_from_command_line
utility.execute()
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/core/management/base.py", line 337, in execute
output = self.handle(*args, **options)
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/core/management/commands/makemigrations.py", line 54, in
handle
loader = MigrationLoader(None)
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/db/migrations/loader.py", line 47, in __init__
self.build_graph()
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/db/migrations/loader.py", line 224, in build_graph
parent = self.check_key(parent, key[0])
File "/Users/valberg/.virtualenvs/django17/lib/python3.4/site-
packages/django/db/migrations/loader.py", line 159, in check_key
raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: testapp
}}}

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

Django

unread,
Jun 16, 2014, 9:02:56 AM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------

Reporter: valberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
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 valberg):

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


Comment:

The summary was misleading. It is not when the migrations get applied, but
when they are about to be created. Basically the 0001_initial.py migration
in `contrib.admin` is looking for an `__first__` migration for the app
that has `AUTH_USER_MODEL`. This does of course not exist because we are
in the process of creating it when this error occurs.

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

Django

unread,
Jun 16, 2014, 9:23:44 AM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* has_patch: 0 => 1


Comment:

I've got rid of the error like this:

{{{#!patch
diff --git a/django/db/migrations/loader.py
b/django/db/migrations/loader.py
index 0d961c2..359d99e 100644
--- a/django/db/migrations/loader.py
+++ b/django/db/migrations/loader.py
@@ -150,6 +150,11 @@ class MigrationLoader(object):
# so we're fine.
return
if key[0] in self.migrated_apps:
+ # Fix #22484 where admin 0001_initial migration relies on a
+ # non-existing migration when running makemigrations with a
custom
+ # user model
+ if key[0] in settings.AUTH_USER_MODEL:
+ return
try:
if key[1] == "__first__":


return list(self.graph.root_nodes(key[0]))[0]
}}}

But I'm not sure if this breaks anything. I'm running the testsuite right
now and will report back. If this is an okay approach I'll make a PR.

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

Django

unread,
Jun 16, 2014, 9:38:48 AM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed

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

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

* needs_tests: 0 => 1


Comment:

Created a PR: https://github.com/django/django/pull/2818

But it needs some tests.

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

Django

unread,
Jun 16, 2014, 11:02:09 AM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed

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

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

* needs_better_patch: 0 => 1


Comment:

Apparently the patch reintroduces #22563 (`ValueError: Lookup failed for
model referenced by field admin.LogEntry.user:`) when running `migrate` in
#22783.

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

Django

unread,
Jun 16, 2014, 12:41:33 PM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by andrewgodwin):

The patch isn't the correct fix; that check is in loader for a reason.

I'm tempted to just say "don't switch the setting till you've made a
migration", as otherwise you are literally telling Django to hang its
contrib apps off of an app with nothing, which is definitely incorrect,
but I'm going to see if I can ignore this error during makemigrations
only.

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

Django

unread,
Jun 16, 2014, 12:59:30 PM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Andrew Godwin <andrew@…>):

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


Comment:

In [changeset:"2b79be2beee8531ab1ef4efca75589705774d8f7"]:
{{{
#!CommitTicketReference repository=""
revision="2b79be2beee8531ab1ef4efca75589705774d8f7"
Fixed #22848: Ignore no-migrations errors during makemigrations only
}}}

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

Django

unread,
Jun 16, 2014, 12:59:45 PM6/16/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

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

In [changeset:"11b8e6154b32c0a46c8910c21070692f3d08f6b4"]:
{{{
#!CommitTicketReference repository=""
revision="11b8e6154b32c0a46c8910c21070692f3d08f6b4"
[1.7.x] Fixed #22848: Ignore no-migrations errors during makemigrations
only
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22848#comment:7>

Django

unread,
Aug 26, 2014, 12:27:25 PM8/26/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by stevejalim):

May I suggest either an extension to this fix or a note in documentation
about it?

Context: I just hit this issue myself, found the ticket, checked my Django
version and thought all should be well, but I thought it still wasn't
working for me until I actually read the patch.

There's still a subtle gotcha where `makemigrations --list` will still
throw the dependency error, while `makemigrations <app name>` will happily
run. So if somoene gets `Dependency on app with no migrations` when
starting runserver and they then try to check their migrations list, they
will be prevented from doing so and they have no cue to as to what they
can do to remedy things.

It's a corner case, sure, but likely will lead to user frustration for
those stuck in that corner.

If you want a patch for the docs, I'd be happy to submit one. I think the
an aside in the custom user docs seems a sensible place. Anyone disagree?

--
Ticket URL: <https://code.djangoproject.com/ticket/22848#comment:8>

Django

unread,
Aug 26, 2014, 3:06:29 PM8/26/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by timgraham):

I'd recommend to create a new ticket as comments on closed tickets often
don't get attention. In any case, I'll be happy to review any docs you
write.

--
Ticket URL: <https://code.djangoproject.com/ticket/22848#comment:9>

Django

unread,
Aug 26, 2014, 6:18:27 PM8/26/14
to django-...@googlegroups.com
#22848: Admin initial migration can't find AUTH_USER_MODEL when running
makemigrations for the first time
---------------------------------+--------------------------------------
Reporter: valberg | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by stevejalim):

Thanks Tim

New ticket #23366 created

--
Ticket URL: <https://code.djangoproject.com/ticket/22848#comment:10>

Reply all
Reply to author
Forward
0 new messages