[Django] #22750: Renaming model fails

27 views
Skip to first unread message

Django

unread,
Jun 2, 2014, 12:15:44 PM6/2/14
to django-...@googlegroups.com
#22750: Renaming model fails
----------------------------+--------------------
Reporter: valberg | 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
----------------------------+--------------------
Create the following models:

{{{
class Foo(models.Model):
pass


class Bar(models.Model):
foo = models.ForeignKey('testapp.Foo')
}}}


Make the migrations:

{{{
$ ./manage.py makemigrations
Migrations for 'testapp':
0001_initial.py:
- Create model Foo
- Create model Bar
}}}

Go in and make the following changes:

{{{
class Baz(models.Model):
pass


class Bar(models.Model):
baz = models.ForeignKey('testapp.Baz')
}}}

Make the new migrations:

{{{
$ ./manage.py makemigrations
Did you rename the testapp.Foo model to Baz? [y/N] y
Did you rename bar.foo to bar.baz (a ForeignKey)? [y/N] y
Migrations for 'testapp':
0002_auto_20140602_1610.py:
- Rename model Foo to Baz
- Rename field foo on bar to baz
- Alter field baz on bar
}}}

The when trying to apply the migrations the following happens:

{{{
$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: sessions, auth, admin, contenttypes
Apply all migrations: testapp
Synchronizing apps without migrations:
Creating tables...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Installing custom SQL...
Installing indexes...
Running migrations:
Applying testapp.0001_initial... OK
Applying testapp.0002_auto_20140602_1610...Traceback (most recent call
last):
File "/home/valberg/Code/projects/django/django/apps/config.py", line
152, in get_model
return self.models[model_name.lower()]
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/valberg/Code/projects/django/django/db/migrations/state.py",
line 76, in render
model = self.apps.get_model(lookup_model[0], lookup_model[1])
File "/home/valberg/Code/projects/django/django/apps/registry.py", line
190, in get_model
return self.get_app_config(app_label).get_model(model_name.lower())
File "/home/valberg/Code/projects/django/django/apps/config.py", line
155, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'testapp' doesn't have a 'foo' 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
"/home/valberg/Code/projects/django/django/core/management/__init__.py",
line 384, in execute_from_command_line
utility.execute()
File
"/home/valberg/Code/projects/django/django/core/management/__init__.py",
line 376, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File
"/home/valberg/Code/projects/django/django/core/management/base.py", line
288, in run_from_argv
self.execute(*args, **options.__dict__)
File
"/home/valberg/Code/projects/django/django/core/management/base.py", line
337, in execute
output = self.handle(*args, **options)
File
"/home/valberg/Code/projects/django/django/core/management/commands/migrate.py",
line 146, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File
"/home/valberg/Code/projects/django/django/db/migrations/executor.py",
line 62, in migrate
self.apply_migration(migration, fake=fake)
File
"/home/valberg/Code/projects/django/django/db/migrations/executor.py",
line 96, in apply_migration
migration.apply(project_state, schema_editor)
File
"/home/valberg/Code/projects/django/django/db/migrations/migration.py",
line 107, in apply
operation.database_forwards(self.app_label, schema_editor,
project_state, new_state)
File
"/home/valberg/Code/projects/django/django/db/migrations/operations/models.py",
line 116, in database_forwards
new_apps = to_state.render()
File "/home/valberg/Code/projects/django/django/db/migrations/state.py",
line 86, in render
model=lookup_model,
ValueError: Lookup failed for model referenced by field testapp.Bar.foo:
testapp.Foo
}}}

Here are the generated migrations:

0001_initial.py:
{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
]

operations = [
migrations.CreateModel(
name='Foo',
fields=[
('id', models.AutoField(verbose_name='ID',
serialize=False, primary_key=True, auto_created=True)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='Bar',
fields=[
('id', models.AutoField(verbose_name='ID',
serialize=False, primary_key=True, auto_created=True)),
('foo', models.ForeignKey(to_field='id',
to='testapp.Foo')),
],
options={
},
bases=(models.Model,),
),
]
}}}

0002_auto_20140602_1610.py
{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('testapp', '0001_initial'),
]

operations = [
migrations.RenameModel(
old_name='Foo',
new_name='Baz',
),
migrations.RenameField(
model_name='bar',
old_name='foo',
new_name='baz',
),
migrations.AlterField(
model_name='bar',
name='baz',
field=models.ForeignKey(to='testapp.Baz', to_field='id'),
),
]
}}}

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

Django

unread,
Jun 2, 2014, 4:03:56 PM6/2/14
to django-...@googlegroups.com
#22750: Renaming model fails
---------------------------------+----------------------------------------
Reporter: valberg | Owner: andrewgodwin
Type: Bug | Status: assigned
Component: Migrations | Version: master
Severity: Release blocker | 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 andrewgodwin):

* status: new => assigned
* severity: Normal => Release blocker
* needs_better_patch: => 0
* needs_tests: => 0
* owner: nobody => andrewgodwin
* needs_docs: => 0


Comment:

This will be solved by the autodetector rewrite I'm currently doing;
claiming to stop other solutions being worked on.

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

Django

unread,
Jun 4, 2014, 1:08:18 PM6/4/14
to django-...@googlegroups.com
#22750: Renaming model fails
---------------------------------+----------------------------------------
Reporter: valberg | Owner: andrewgodwin
Type: Bug | Status: assigned
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* has_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

[https://github.com/django/django/pull/2760 Pull request] for auto
detector rewrite. Testing of the branch is welcome.

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

Django

unread,
Jun 6, 2014, 2:24:38 AM6/6/14
to django-...@googlegroups.com
#22750: Renaming model fails
---------------------------------+----------------------------------------
Reporter: valberg | Owner: andrewgodwin
Type: Bug | Status: assigned
Component: Migrations | Version: master

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

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

Comment (by andrewgodwin):

Unfortunately it looks like the problem also involves the RenameModel
operation not affecting incoming FKs; this will not be fixed by the
autodetector merge, I've just tested to make sure.

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

Django

unread,
Jun 7, 2014, 9:18:33 PM6/7/14
to django-...@googlegroups.com
#22750: Renaming model fails
---------------------------------+----------------------------------------
Reporter: valberg | Owner: andrewgodwin
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed

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

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

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


Comment:

In [changeset:"4ce7a6bc84c68406e39f48550434faeef3277eba"]:
{{{
#!CommitTicketReference repository=""
revision="4ce7a6bc84c68406e39f48550434faeef3277eba"
Fixed #22750, #22248: Model renaming now also alters field FKs
}}}

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

Django

unread,
Jun 7, 2014, 9:18:50 PM6/7/14
to django-...@googlegroups.com
#22750: Renaming model fails
---------------------------------+----------------------------------------
Reporter: valberg | Owner: andrewgodwin
Type: Bug | Status: closed
Component: Migrations | Version: master

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

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

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

In [changeset:"b568bdf8da30fe5940a31add9f3a2b8029ecb04d"]:
{{{
#!CommitTicketReference repository=""
revision="b568bdf8da30fe5940a31add9f3a2b8029ecb04d"
[1.7.x] Fixed #22750, #22248: Model renaming now also alters field FKs
}}}

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

Reply all
Reply to author
Forward
0 new messages