#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.49dcc14de8cae66c2b7cbeab856c73ba%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.