Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#22750 closed Bug (fixed)

Renaming model fails

Reported by: valberg Owned by: andrewgodwin
Component: Migrations Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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'),
        ),
    ]

Change History (5)

comment:1 Changed 13 months ago by andrewgodwin

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to andrewgodwin
  • Patch needs improvement unset
  • Severity changed from Normal to Release blocker
  • Status changed from new to assigned

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

comment:2 Changed 13 months ago by timo

  • Has patch set
  • Triage Stage changed from Unreviewed to Accepted

Pull request for auto detector rewrite. Testing of the branch is welcome.

comment:3 Changed 13 months ago 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.

comment:4 Changed 13 months ago by Andrew Godwin <andrew@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 4ce7a6bc84c68406e39f48550434faeef3277eba:

Fixed #22750, #22248: Model renaming now also alters field FKs

comment:5 Changed 13 months ago by Andrew Godwin <andrew@…>

In b568bdf8da30fe5940a31add9f3a2b8029ecb04d:

[1.7.x] Fixed #22750, #22248: Model renaming now also alters field FKs

Note: See TracTickets for help on using tickets.
Back to Top