Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23160 closed Bug (fixed)

KeyError when performing a RenameModel migration operation for a model with self-referential FK field

Reported by: whitews@… Owned by: Simon Charette
Component: Migrations Version: 1.7-rc-2
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Simon Charette)

I'm simply trying to rename a model which has a relationship to itself. Running the RenameModel operation yields the following traceback:

$ python manage.py migrate myapp
Operations to perform:
  Apply all migrations: myapp
Running migrations:
  Applying myapp.0002_rename_Foo_to_Bar...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/executor.py", line 91, in apply_migration
    if self.detect_soft_applied(migration):
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/executor.py", line 134, in detect_soft_applied
    project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/loader.py", line 268, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/graph.py", line 147, in make_state
    project_state = self.nodes[node].mutate_state(project_state)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/migration.py", line 76, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7c2-py2.7.egg/django/db/migrations/operations/models.py", line 132, in state_forwards
    for name, field in state.models[related_key].fields:
KeyError: ('myapp', 'foo')

Attachments (3)

models_before.py (503 bytes ) - added by whitews@… 10 years ago.
initial models before renaming
models_after.py (503 bytes ) - added by whitews@… 10 years ago.
models.py after renaming model
0002_rename_Foo_to_Bar.py (281 bytes ) - added by whitews@… 10 years ago.
migration file to rename model

Download all attachments as: .zip

Change History (12)

by whitews@…, 10 years ago

Attachment: models_before.py added

initial models before renaming

by whitews@…, 10 years ago

Attachment: models_after.py added

models.py after renaming model

by whitews@…, 10 years ago

Attachment: 0002_rename_Foo_to_Bar.py added

migration file to rename model

comment:1 by Simon Charette, 10 years ago

Description: modified (diff)
Owner: changed from nobody to Simon Charette
Severity: NormalRelease blocker
Status: newassigned

Reproduced against master.

comment:2 by Baptiste Mispelon, 10 years ago

Same here.

For what it's worth, you can trigger the error with one simple model:

class Foo(models.Model):
    parent = models.ForeignKey('self')

Renaming that model to Bar and running makemigrations works: the autodetector asks if the model was renamed.
However, running migrate after that fails with the reported KeyError.

comment:3 by Simon Charette, 10 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

Created a PR with tests.

comment:4 by anonymous, 10 years ago

Verified the PR from charettes fixes the issue. Thanks!

comment:5 by Simon Charette, 10 years ago

Triage Stage: AcceptedReady for checkin

Marking RFC per Florian's comment on IRC. Will just wait for the CI build to complete and I'll merge/backport to 1.7.x.

comment:6 by Simon Charette <charette.s@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In cbb29af1aa1ebe11b38e73a6f34df9cd171d6c5b:

Fixed #23160 -- Correctly rename models with self referential fields.

Thanks to whitews AT gmail for the report.

comment:7 by Simon Charette <charette.s@…>, 10 years ago

In 1b00738f73444693dd94958f3fc53c089f4d96e7:

Fixed a MySQL test failure introduced by cbb29af1aa.

Seems like unlike other backends MySQL get_constraints fails when dealing
with a non-existing table. refs #23160.

comment:8 by Simon Charette <charette.s@…>, 10 years ago

In 6b2473d3e8048158dbc8c886d755cbf948325077:

[1.7.x] Fixed #23160 -- Correctly rename models with self referential fields.

Thanks to whitews AT gmail for the report.

Backport of cbb29af1aa from master

comment:9 by Simon Charette <charette.s@…>, 10 years ago

In 99f3a65c730f643d35d959aa9a693101b2d21efd:

[1.7.x] Fixed a MySQL test failure introduced by cbb29af1aa.

Seems like unlike other backends MySQL get_constraints fails when dealing
with a non-existing table. refs #23160.

Backport of 1b00738f73 from master

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