Opened 9 years ago

Last modified 9 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

Description

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 "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "d:\develop\myproj\django-trunk\django\core\management\__init__.py", line 330, in execute_from_command_line
    utility.execute()
  File "d:\develop\myproj\django-trunk\django\core\management\__init__.py", line 322, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "d:\develop\myproj\django-trunk\django\core\management\base.py", line 350, in run_from_argv
    self.execute(*args, **cmd_options)
  File "d:\develop\myproj\django-trunk\django\core\management\base.py", line 401, in execute
    output = self.handle(*args, **options)
  File "d:\develop\myproj\django-trunk\django\core\management\commands\migrate.py", line 187, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "d:\develop\myproj\django-trunk\django\db\migrations\executor.py", line 89, in migrate
    state = migration.mutate_state(state)  # state is cloned inside
  File "d:\develop\myproj\django-trunk\django\db\migrations\migration.py", line 78, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "d:\develop\myproj\django-trunk\django\db\migrations\operations\fields.py", line 50, in state_forwards
    state.reload_model(app_label, self.model_name_lower)
  File "d:\develop\myproj\django-trunk\django\db\migrations\state.py", 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\state.py", 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/0001_initial.py:

        migrations.AddField(
            model_name='somemodel',
            name='somefieldname',
            field=models.ForeignKey(to='myapp.AValidModel'),
            preserve_default=True,
        ),

and in myapp/migrations/0002_a.py:

        migrations.RemoveField(
            model_name='somemodel',
            name='somefieldname',
        ),

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 mutation.py:78 (mutate_state()), I can see that the "current migration" when this occurs is a 0002_b.py, i.e. another migration dependent on 0001_initial.py 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