[Django] #23093: Running migrations on an existing database with a custom user model fails

8 views
Skip to first unread message

Django

unread,
Jul 24, 2014, 10:56:27 AM7/24/14
to django-...@googlegroups.com
#23093: Running migrations on an existing database with a custom user model fails
----------------------------+--------------------
Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
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).

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

Django

unread,
Jul 24, 2014, 10:59:46 AM7/24/14
to django-...@googlegroups.com
#23093: Running migrations on an existing database with a custom user model fails
---------------------------------+------------------------------------

Reporter: magopian | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_docs: => 0
* severity: Normal => Release blocker
* needs_tests: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Jul 25, 2014, 12:36:13 PM7/25/14
to django-...@googlegroups.com
#23093: Running migrations on an existing database with a custom user model fails
---------------------------------+------------------------------------
Reporter: magopian | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Andrew Godwin <andrew@…>):

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


Comment:

In [changeset:"a64bc3df77e074ddb7aa5c42eb2a84c26917a3b7"]:
{{{
#!CommitTicketReference repository=""
revision="a64bc3df77e074ddb7aa5c42eb2a84c26917a3b7"
Fixed #23093: soft application detection for swapped models
}}}

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

Django

unread,
Jul 25, 2014, 12:36:30 PM7/25/14
to django-...@googlegroups.com
#23093: Running migrations on an existing database with a custom user model fails
---------------------------------+------------------------------------
Reporter: magopian | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master

Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

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

In [changeset:"919d10e619872b56a4d6bc91ccd031b90775a437"]:
{{{
#!CommitTicketReference repository=""
revision="919d10e619872b56a4d6bc91ccd031b90775a437"
[1.7.x] Fixed #23093: soft application detection for swapped models
}}}

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

Reply all
Reply to author
Forward
0 new messages