[Django] #24506: Proxying User model creates an invalid migration

29 views
Skip to first unread message

Django

unread,
Mar 19, 2015, 9:35:34 AM3/19/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
----------------------------------------------+--------------------
Reporter: tinloaf | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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.

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

Django

unread,
Mar 19, 2015, 1:29:16 PM3/19/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------

Reporter: tinloaf | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Could you provide a minimal project that we could download to reproduce
the issue? Did you test with Django 1.8?

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

Django

unread,
Mar 22, 2015, 7:06:15 AM3/22/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: assigned

Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | 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 marcofucci):

* status: new => assigned
* owner: nobody => marcofucci
* stage: Unreviewed => Accepted


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

Django

unread,
Mar 22, 2015, 8:01:49 AM3/22/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
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 marcofucci):

The bug seems to have been fixed in Django 1.8.

I've created a github repo https://github.com/marcofucci/django-
ticket-24506 containing two projects using the same code but different
django versions.

The one using django 1.7 breaks and the one using django 1.8 doesn't.

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

Django

unread,
Mar 22, 2015, 8:03:19 AM3/22/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: closed

Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | 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 marcofucci):

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


--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:4>

Django

unread,
May 21, 2015, 7:50:18 AM5/21/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: new
Component: Database layer | Version: 1.8

(models, ORM) |
Severity: Normal | 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 Ernest0x):

* status: closed => new
* version: 1.7 => 1.8
* resolution: fixed =>


Comment:

I get exactly the same exception chain on 1.8.2 when trying to migrate an
app that contains a model with a foreign key to another app's model
defined as a proxy model of Django's auth application Group model. In case
this is relevant, the application that contains the proxy model has a
custom but very simple AppConfig subclass, that only overrides ready()
method just to import a 'signals' module.

--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:5>

Django

unread,
May 21, 2015, 7:56:46 AM5/21/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
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 Ernest0x):

The traceback occurs when rendering model states:

{{{
Operations to perform:
Synchronize unmigrated apps: rest_framework, django_python3_ldap,
debug_toolbar, messages, staticfiles
Apply all migrations: contenttypes, users, issues, ...


Synchronizing apps without migrations:
Creating tables...

Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states...Traceback (most recent call last):
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/apps/config.py", line 159, in get_model
return self.models[model_name.lower()]
KeyError: 'group'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/path/to/virtualenv/lib/python3.4/site-
packages/django/db/migrations/state.py", line 238, in __init__
model = self.get_model(lookup_model[0], lookup_model[1])
File "/path/to/virtualenv/lib/python3.4/site-


packages/django/apps/registry.py", line 202, in get_model
return self.get_app_config(app_label).get_model(model_name.lower())

File "/path/to/virtualenv/lib/python3.4/site-
packages/django/apps/config.py", line 162, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'users' doesn't have a 'group' model.

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 "/path/to/virtualenv/lib/python3.4/site-
packages/django/core/management/__init__.py", line 338, in
execute_from_command_line
utility.execute()
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/db/migrations/executor.py", line 100, in migrate
state.apps # Render all real_apps -- performance critical
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/db/migrations/state.py", line 166, in apps
return StateApps(self.real_apps, self.models)
File "/path/to/virtualenv/lib/python3.4/site-
packages/django/db/migrations/state.py", line 248, in __init__


raise ValueError(msg.format(field=operations[0][1],
model=lookup_model))
ValueError: Lookup failed for model referenced by field

issues.SomeModel.group: users.Group
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:6>

Django

unread,
May 21, 2015, 2:11:18 PM5/21/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
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 timgraham):

Could you please add a minimal project that reproduces the issue?

--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:7>

Django

unread,
May 25, 2015, 3:13:14 AM5/25/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | 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 Ernest0x):

* Attachment "myproject.tar.gz" added.

Django

unread,
May 25, 2015, 3:34:45 AM5/25/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
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 Ernest0x):

I added an example project as an attachment. Running 'migrate' reproduces
the problem. The traceback above is shown when running the 'migrate'
command for a second time as well as for subsequent runs. The first run
also produces a traceback, which seems to be the same error but triggered
in a different code path.

The project's history/migrations:
1) 'myapp' application has a MyModel model with a text field / The
Django's default user model is being used (no AUTH_USER_MODEL is set).
2) 'myapp' application's MyModel gets a foreign key to 'auth.Group'
3) 'users' application is added to INSTALLED_APPS. It contains a custom
User model.
4) 'users' application gets a new Group model as a proxy to auth.Group and
MyModel's foreign key is changed to point to 'users.Group'.
AUTH_USER_MODEL is changed to 'users.User'
5) 'myapp' application's MyModel gets a new text field. Migration file for
this change is created without problems, but running 'migrate' fails with
the trace above.

--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:8>

Django

unread,
May 25, 2015, 3:13:48 PM5/25/15
to django-...@googlegroups.com
#24506: Proxying User model creates an invalid migration
-------------------------------------+-------------------------------------
Reporter: tinloaf | Owner:
| marcofucci
Type: Bug | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | 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 Ernest0x):

* cc: ernest0x@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:9>

Django

unread,
May 26, 2015, 6:54:17 AM5/26/15
to django-...@googlegroups.com
#24506: Migrations are crashing after changing foreign key from auth.Group model to
a proxy model of auth.Group
----------------------------+--------------------------------------
Reporter: tinloaf | Owner: marcofucci
Type: Bug | Status: new
Component: Migrations | Version: 1.8

Severity: Normal | 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 Ernest0x):

* component: Database layer (models, ORM) => Migrations


--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:10>

Django

unread,
May 28, 2015, 9:31:48 AM5/28/15
to django-...@googlegroups.com
#24506: Migrations are crashing after changing foreign key from auth.Group model to
a proxy model of auth.Group
----------------------------+--------------------------------------
Reporter: tinloaf | Owner: marcofucci
Type: Bug | Status: new

Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
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 marcofucci):

I had a good look at this.
The problem has nothing to do with the auth app and proxy models.

The issue is caused by `AlterField` not being able to build dependencies
properly.
If you take a look at the attached project, you'll realise that
`myproject.myapp.migrations.0003_auto_20150524_2202.py` should depend on
`('users', '0002_group')`.

If you do add that to the `dependencies` list, the `migrate` command
works.

I'm pretty sure this is a different bug. I tried to search in Trac, there
are many related bugs but nothing exactly like this.
The best one might be #22944 .

Maybe @andrewgodwin can take a look at this and close it referencing the
right ticket as he worked on many of them?
I've actually found a way to fix it but I don't have enough time atm.

--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:11>

Django

unread,
Dec 24, 2015, 4:04:33 AM12/24/15
to django-...@googlegroups.com
#24506: Migrations are crashing after changing foreign key from auth.Group model to
a proxy model of auth.Group
----------------------------+--------------------------------------
Reporter: tinloaf | Owner: marcofucci
Type: Bug | Status: new

Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
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 auvipy):

I deleted all old migrations and ran makemigrations and migrate to fix the
issue in django 1.8.7

--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:12>

Django

unread,
Jun 3, 2022, 6:12:07 AM6/3/22
to django-...@googlegroups.com
#24506: Migrations are crashing after changing foreign key from auth.Group model to
a proxy model of auth.Group
----------------------------+------------------------------------
Reporter: tinloaf | Owner: (none)
Type: Bug | Status: new

Component: Migrations | Version: 1.8
Severity: Normal | 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 Mariusz Felisiak):

* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:14>

Django

unread,
Jun 3, 2022, 6:12:13 AM6/3/22
to django-...@googlegroups.com
#24506: Migrations are crashing after changing foreign key from auth.Group model to
a proxy model of auth.Group
----------------------------+------------------------------------
Reporter: tinloaf | Owner: (none)
Type: Bug | Status: assigned

Component: Migrations | Version: 1.8
Severity: Normal | 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 Mariusz Felisiak):

* owner: Marco Fucci => (none)


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24506#comment:13>

Reply all
Reply to author
Forward
0 new messages