﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23093	Running migrations on an existing database with a custom user model fails	Mathieu Agopian	nobody	"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)."	Bug	closed	Migrations	dev	Release blocker	fixed			Accepted	0	0	0	0	0	0
