Opened 8 years ago

Last modified 8 years ago

#24225 closed Bug

KeyError when migrating in 1.8a1/master@728b6fd (does not occur in 1.7.3) — at Initial Version

Reported by: Henrik Heimbuerger Owned by: nobody
Component: Migrations Version: 1.8alpha1
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


When running the migrate management command (even if there is nothing to migrate) on 1.8a1 or the current GitHub master, I'm getting the following uncaught KeyError:

Running migrations:
  Rendering model states... DONE
Traceback (most recent call last):
  File "", line 10, in <module>
  File "d:\develop\myproj\django-trunk\django\core\management\", line 330, in execute_from_command_line
  File "d:\develop\myproj\django-trunk\django\core\management\", line 322, in execute
  File "d:\develop\myproj\django-trunk\django\core\management\", line 350, in run_from_argv
    self.execute(*args, **cmd_options)
  File "d:\develop\myproj\django-trunk\django\core\management\", line 401, in execute
    output = self.handle(*args, **options)
  File "d:\develop\myproj\django-trunk\django\core\management\commands\", line 187, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "d:\develop\myproj\django-trunk\django\db\migrations\", line 89, in migrate
    state = migration.mutate_state(state)  # state is cloned inside
  File "d:\develop\myproj\django-trunk\django\db\migrations\", line 78, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "d:\develop\myproj\django-trunk\django\db\migrations\operations\", line 50, in state_forwards
    state.reload_model(app_label, self.model_name_lower)
  File "d:\develop\myproj\django-trunk\django\db\migrations\", line 61, in reload_model
    self._reload_one_model(rel_model._meta.app_label, rel_model._meta.model_name)
  File "d:\develop\myproj\django-trunk\django\db\migrations\", line 68, in _reload_one_model
    self.models[app_label, model_name].render(self.apps)
KeyError: ('myapp', 'anoldmodel')

The reported model is created and then only appears twice in my migrations:

In myapp/migrations/


and in myapp/migrations/


Other than that, the model hasn't been changed in the migrations.

The application is closed source and I can't share real code with you unfortunately.

I do have a debugger connected to the issue, but I have no idea where to even start debugging this. By checking the stack frame for (mutate_state()), I can see that the "current migration" when this occurs is a, i.e. another migration dependent on which therefore shouldn't be aware of the field removal yet.

These migrations do work on 1.7.3. (But I can't really work on 1.7.3 because unit test startup takes minutes without --keepdb…)

Whatever the cause of this is, I highly recommend catching KeyError in _reload_one_model() and rethrowing a proper exception with a descriptive error message, including the stage and specific migration at which this occurs. Because a KeyError being raised somewhere in the depths of the migration system tells me nothing about what or where the issue is.

Change History (0)

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