﻿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
24506	Migrations are crashing after changing foreign key from auth.Group model to a proxy model of auth.Group	tinloaf		"Hi! 

I have created a proxy model for the User model like this (in core.models):

{{{
from django.contrib.auth.models import User as OriginalUser

class User(OriginalUser):
	objects = UserManager()
	all_objects = AllUserManager()

	class Meta:
		proxy = True
[...]
}}}

I then changed some ForeignKeys that have been pointing to the original User to my proxy. Now makemigrations creates a migration that looks like this:

{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('finance', '0011_auto_20141126_1447'),
    ]

    operations = [
        migrations.AlterField(
            model_name='financialdata',
            name='user',
            field=models.OneToOneField(to='core.User'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='mandate',
            name='user',
            field=models.ForeignKey(to='core.User'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='userrate',
            name='user',
            field=models.ForeignKey(to='core.User'),
            preserve_default=True,
        ),
    ]
}}}


Which looks OK so far. If I try to run this migration, it fails like this:

{{{
(venv)tinloaf@janeway alumnet % python manage.py migrate                         
Operations to perform:
  Synchronize unmigrated apps: gis, bootstrap3, dajaxice, autocomplete_light
  Apply all migrations: sessions, ml, auth, admin, local, finance, core, contenttypes
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying finance.0012_auto_20150319_1316...Traceback (most recent call last):
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/apps/registry.py"", line 148, in get_app_config
    return self.app_configs[app_label]
KeyError: 'core'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/migrations/state.py"", line 84, in render
    model = self.apps.get_model(lookup_model[0], lookup_model[1])
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/apps/registry.py"", line 202, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/apps/registry.py"", line 150, in get_app_config
    raise LookupError(""No installed app with label '%s'."" % app_label)
LookupError: No installed app with label 'core'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/core/management/__init__.py"", line 385, in execute_from_command_line
    utility.execute()
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/core/management/__init__.py"", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/core/management/base.py"", line 338, in execute
    output = self.handle(*args, **options)
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/core/management/commands/migrate.py"", line 161, in handle
    executor.migrate(targets, plan, fake=options.get(""fake"", False))
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/migrations/executor.py"", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/migrations/executor.py"", line 96, in apply_migration
    if self.detect_soft_applied(migration):
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/migrations/executor.py"", line 140, in detect_soft_applied
    apps = project_state.render()
  File ""/mnt/data/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/migrations/state.py"", line 94, in render
    raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
ValueError: Lookup failed for model referenced by field finance.FinancialData.user: core.User
}}}

So, for some reason, it cannot find the core module and/or the core.User model. The core module is in my installed apps, and the whole application works if I just fake the migration, so the model itself works, etc. Still, having to fake a migration in this case should probably not be the intended solution.

Also, I can't fake that migration in tests, making testing essentially impossible in this case."	Bug	new	Migrations	1.8	Normal			ernest0x@…	Accepted	0	0	0	0	0	0
