Django 迁移错误 KeyError: ('list', u'user')

新手上路,请多包涵

我想跑

python manage.py migrate

或者

python manage.py makemigrations

我收到此错误:

 Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 183, in handle
    executor.loader.project_state(),
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/loader.py", line 338, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/graph.py", line 280, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/migration.py", line 88, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "/Users/rostunov/temp/venv/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 547, in state_forwards
    model_state = state.models[app_label, self.name_lower]
KeyError: ('list', u'user')

它发生在我从 git 中提取我的应用程序的另一个版本之后。

我在另一台机器上使用相同的代码没有这个错误。我尝试将 --fakezerosquashmigrations 一起使用,但这也无济于事。

无法得到如何解决它。

原文由 Roberto 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 730
2 个回答

问题出在迁移文件中。当我以某种方式提交到 git 时,我删除了一个迁移文件,所以顺序就像 0001 0003 0004 没有 0002 。在第二个迁移文件中,我创建了一个名为 user 的模型。

问题是当我运行 python manage.py migrate django 找不到创建名为 user 的模型的地方(这个模型是在 0002 文件中创建的)。

我通过手动将此代码添加到 0001 迁移文件来解决它:

 migrations.CreateModel(
        name='user',
        fields=[
            (...necessary fields...),
        ],
        options={
            'ordering': ('title',),
        },
    ),

原文由 Roberto 发布,翻译遵循 CC BY-SA 3.0 许可协议

我遇到了一个类似的问题,其中 db\migrations\operations\models.py --- 在通过 PyCharm 的重构(重命名)重命名模型后抛出 KeyError

显然,重构也发生在迁移文件中。打开迁移文件并改回原始命名时, makemigrations 命令运行良好。

原文由 SaeX 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题