Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#23182 closed Bug (needsinfo)

squashmigrations having trouble with auth.User foreign key

Reported by: wkschwartz@… Owned by: nobody
Component: Migrations Version: 1.7-rc-2
Severity: Normal Keywords: migrations, squashmigrations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Django 1.7rc2 on SQLite and Python 3.4. I'm trying to use squashmigrations. It broke, even with --no-optimize. The documentation said to report bugs.

My app myapp has a model MyModel with a couple foreign keys to django.contrib.auth.User (yes, auth is in my INSTALLED_APPS, despite what the stack trace below says; notably everything was working fine before squashing):

from django.contrib.auth.models import User
from django.db.models import Model, ForeignKey

class MyModel(Model):
    creator = ForeignKey(User, related_name='mymodel_creator_set')
    modifier = ForeignKey(User, related_name='mymodel_modifier_set')
    # ... and then a bunch of business logic...

I have a few other models with foreign keys to User, but that's the one that shows up in the stack trace.

I had accumulated 18 migrations, which were slowing down my tests (the migrations always run at the start of test, at least when developing on SQLite). So I ran

(venv) $ ./ squashmigrations myapp 0018

Then when I ran

(venv) $ rm db.sqlite3 && ./ migrate

I got the stack trace below. When I deleted the squashed migration and reran it with --no-optimize, I got exactly the same stack trace.

(venv3) $ rm db.sqlite3 && ./ migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, myapp
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying myapp.0001_squashed_0018_<redacted>...Traceback (most recent call last):
  File ".../django/apps/", line 136, in get_app_config
    return self.app_configs[app_label]
KeyError: 'auth'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../django/db/migrations/", line 79, in render
    model = self.apps.get_model(lookup_model[0], lookup_model[1])
  File ".../django/apps/", line 190, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File ".../django/apps/", line 138, in get_app_config
    raise LookupError("No installed app with label '%s'." % app_label)
LookupError: No installed app with label 'auth'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./", line 10, in <module>
  File ".../django/core/management/", line 385, in execute_from_command_line
  File ".../django/core/management/", line 377, in execute
  File ".../django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ".../django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File ".../django/core/management/commands/", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File ".../django/db/migrations/", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File ".../django/db/migrations/", line 90, in apply_migration
    if self.detect_soft_applied(migration):
  File ".../django/db/migrations/", line 134, in detect_soft_applied
    apps = project_state.render()
  File ".../django/db/migrations/", line 89, in render
ValueError: Lookup failed for model referenced by field myapp.MyModel.modifier: auth.User

Change History (4)

comment:2 Changed 6 years ago by Tim Graham

Description: modified (diff)

comment:3 Changed 6 years ago by Tim Graham

Could you try with the latest stable/1.7.x. Also a minimal project that we could download with steps to reproduce would be really helpful.

comment:4 Changed 6 years ago by Tim Graham

Resolution: needsinfo
Status: newclosed

Please reopen if you can provide additional details.

comment:5 Changed 6 years ago by William Schwartz

Sorry I haven't been very helpful -- completely slammed at work. For what it's worth, I reran the migration with stable/1.7.x @ 67870137b and the squash worked. I was unable to come up with a test case that wasn't my whole project.

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