Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#21142 closed Bug (fixed)

Cannot run migrations that reference django.contrib.auth.models.User

Reported by: mario.pfeifer@… Owned by: Andrew Godwin <andrew@…>
Component: Migrations Version: master
Severity: Normal Keywords: Migration, Auth
Cc: OBu, robinchew@…, zxc.spam@…, Héctor Urbina Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

To verify this bug I use a very simple model in which I reference to the User model:

from django.conf import settings
from django.db import models


class Article(models.Model):
    name = models.CharField('Name', max_length=255)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)

After running ./manage.py makemigrations everything looks fine:

Do you want to enable migrations for app 'articles'? [y/N] y
Migrations for 'articles':
  0001_initial.py:
    - Create model Article

But running ./manage.py migrate runs into following error:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mario/django-trunk/django/core/management/__init__.py", line 397, in execute_from_command_line
    utility.execute()
  File "/home/mario/django-trunk/django/core/management/__init__.py", line 390, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mario/django-trunk/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/mario/django-trunk/django/core/management/base.py", line 289, in execute
    output = self.handle(*args, **options)
  File "/home/mario/django-trunk/django/core/management/commands/migrate.py", line 88, in handle
    targets = executor.loader.graph.leaf_nodes()
  File "/home/mario/django-trunk/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/mario/django-trunk/django/db/migrations/loader.py", line 169, in graph
    graph.add_dependency(key, parent)
  File "/home/mario/django-trunk/django/db/migrations/graph.py", line 40, in add_dependency
    raise KeyError("Dependency references nonexistent parent node %r" % (parent,))
KeyError: "Dependency references nonexistent parent node (u'auth', '0001_initial')"

The same happens when I directly reference to the User model:

from django.contrib.auth.models import User
from django.db import models


class Article(models.Model):
    name = models.CharField('Name', max_length=255)
    author = models.ForeignKey(User)

Change History (14)

comment:1 Changed 3 years ago by Baptiste Mispelon

Triage Stage: UnreviewedAccepted

Hi,

I can reproduce the issue and from what I can tell, you're following the documentation (both for referencing the user model and for making/applying migrations) so this is most likely a bug.

Thanks for the detailed report.

comment:2 Changed 3 years ago by OBu

I just ran into the same problem.
To add one more detail to the problem description:

0001_initial.py starts with

class Migration(migrations.Migration):

    dependencies = [(u'auth', '0001_initial'), (u'my_app_name', '__first__')]

and, of course, there are no migrations in "auth".

Last edited 3 years ago by OBu (previous) (diff)

comment:3 Changed 3 years ago by Tim Graham

Summary: Referencing the User model and migrationsCannot run migrations that reference django.contrib.auth.models.User

I've looked into adding migrations to contrib.auth, but ran into #21275. It's also unclear how migrations are supposed to work with "swappable" models like User. Obviously, we want to ignore it if a project uses a custom user.

comment:4 Changed 3 years ago by OBu

...and to add to the list of problems: There is no migration __first__as well.
Removing the dependencies does not help since migrate is checking all foreign keys and it does not find information about foreign keys pointing to Users.

Since tried to add a --fake migration since in my current application I wanted to add just a boolean, but this still did not help since migrate traverses through all models fields.

To sum it up: No work arounds found, switching back to south :-(...

I don't have too much experience with django core development, but I would rate this bug a "release blocker"...

comment:5 Changed 3 years ago by Tim Graham

Yes, migrations is still under active development. It was merged well before it was feature complete because reconciling changes on master with the external branch was a challenge. We will definitely review all migrations tickets before releasing 1.7.

comment:6 Changed 3 years ago by Tim Graham

From Andrew on IRC: "My plan was that contrib apps would ship with migrations. Specifically, I'd like to ship auth with some straight in 1.7. Initial fake migrations will be handled by the new framework directly."

So this bug should be fixed in due time before the 1.7 release. Thanks for your patience.

comment:7 Changed 3 years ago by OBu

Timo, thanks for keeping us updated! I'm really looking forward to the new migration system!

comment:8 Changed 3 years ago by OBu

Cc: OBu added

comment:9 Changed 3 years ago by Andrew Godwin

Yes, sorry, you probably hit a part of the dependency resolution that was failing. It's supposed to depend on first, which if the application doesn't have migrations is ignored - I'll make sure it's doing that.

comment:10 Changed 3 years ago by Robin

Cc: robinchew@… added

comment:11 Changed 3 years ago by zxc.spam@…

Cc: zxc.spam@… added

comment:12 Changed 3 years ago by Héctor Urbina

Cc: Héctor Urbina added

comment:13 Changed 3 years ago by Andrew Godwin <andrew@…>

Owner: set to Andrew Godwin <andrew@…>
Resolution: fixed
Status: newclosed

In 64887c644a8fe1004a15e27087d4e27c95c90fcd:

Fixed #21142: Dependency failures on unmigrated apps.

comment:14 Changed 3 years ago by anthony.arnold@…

Was just Googling this error and discovered that it was pulled to master an hour and a half ago. Thanks, Internet.

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