Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23093 closed Bug (fixed)

Running migrations on an existing database with a custom user model fails

Reported by: Mathieu Agopian Owned by: nobody
Component: Migrations Version: dev
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Steps to reproduce:
1/ django-admin startproject foo
2/ cd foo && ./manage.py startapp bar
3/ modify foo/settings.py:

  • add bar to the INSTALLED_APPS
  • add AUTH_USER_MODEL = 'bar.User'

4/ edit bar/models.py:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

5/ run the migrations a first time to create the tables:

./manage.py makemigrations && ./manage.py migrate

6/ drop the django_migrations table, to pretend we're coming from an old project upgrading to django1.7

echo "drop table django_migrations;" | ./manage.py dbshell

7/ run the migrations again, and watch it fail:

./manage.py makemigrations && ./manage.py migrate

This will result in:

Operations to perform:
  Apply all migrations: admin, contenttypes, bar, auth, sessions
Running migrations:
  Applying auth.0001_initial...Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute_from_command_line
    utility.execute()
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 322, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/core/management/base.py", line 363, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/core/management/base.py", line 412, in execute
    output = self.handle(*args, **options)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 270, in create_model
    self.execute(sql, params)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 98, in execute
    cursor.execute(sql, params)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 78, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql, params)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql, params)
  File "/home/mathieu/.virtualenvs/bugmig/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: table "auth_permission" already exists

This is on django trunk. I'm not sure, but this looks like a release blocker :/

From my first investigations, it seems to be a "fake migration detection" issue, in django/db/migrations/executor.py, in the detect_soft_applied method.

It's seeing a "CreateModel" on "auth.User" (from the initial migration of the django.contrib.auth.user app), and not seeing the auth_user table. I believe there should be a special case if the CreateModel has the swappable option (it should then check the swappable's table instead of auth_user).

Change History (3)

comment:1 by Tim Graham, 10 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:2 by Andrew Godwin <andrew@…>, 10 years ago

Resolution: fixed
Status: newclosed

In a64bc3df77e074ddb7aa5c42eb2a84c26917a3b7:

Fixed #23093: soft application detection for swapped models

comment:3 by Andrew Godwin <andrew@…>, 10 years ago

In 919d10e619872b56a4d6bc91ccd031b90775a437:

[1.7.x] Fixed #23093: soft application detection for swapped models

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