#24225: KeyError when migrating in 1.8a1/master@728b6fd (does not occur in
1.7.3)
------------------------------+--------------------------------------
Reporter: hheimbuerger | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by hheimbuerger):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old 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.
New 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', 'somemodel')
}}}
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24225#comment:1>
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/070.31a049130687984d43e358ed5bf07841%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.