【问题标题】:Django - A model can't have more than one AutoFieldDjango - 一个模型不能有多个 AutoField
【发布时间】:2015-10-14 13:28:54
【问题描述】:

我开始随机出现这个问题。我知道 Django 会生成自己的 id,但是我的很多代码一直在使用一直在工作的自定义 AutoFields。我今天添加了一个新课程并尝试进行迁移,但此错误不断出现。

我确实删除了 AutoFields 的所有实例并重试迁移,但问题仍然存在,这让我相信它是别的东西......我不相信我的 Django 版本根本没有改变......

错误:

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 390,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrat
ions.py", line 98, in handle
    loader.project_state(),
  File "C:\Python34\lib\site-packages\django\db\migrations\loader.py", line 326,
 in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self
.unmigrated_apps))
  File "C:\Python34\lib\site-packages\django\db\migrations\graph.py", line 231,
in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)

  File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 8
3, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py"
, line 51, in state_forwards
    state.reload_model(app_label, self.model_name_lower)
  File "C:\Python34\lib\site-packages\django\db\migrations\state.py", line 152,
in reload_model
    self.apps.render_multiple(states_to_be_rendered)
  File "C:\Python34\lib\site-packages\django\db\migrations\state.py", line 262,
in render_multiple
    model.render(self)
  File "C:\Python34\lib\site-packages\django\db\migrations\state.py", line 546,
in render
    body,
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 189, in __
new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 324, in ad
d_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 989, in contribute_to_class
    "A model can't have more than one AutoField."
AssertionError: A model can't have more than one AutoField.

这是我的一个领域的一个例子:

assetid = models.AutoField(primary_key=True)

我的很多代码已经依赖于名称本身,因此更改它将是一个大问题。此外,这之前工作得很好!我现在似乎无法迁移它。我应该提到我使用 sqlite3 db。

【问题讨论】:

  • 您可能希望将迁移文件添加到您的问题和新类(模型?)中。
  • 解决了它,删除新的迁移文件(我相信不知何故搞砸了迁移)工作并且我重新提交了它。如果您提出快速回答建议,请将其标记为已回答

标签: python django models


【解决方案1】:

这是因为 Django 默认使用 AutoField 作为 id .....所以,如果您希望其他字段为 AutoField,请确保确认 primary_key=True。 这样做会从数据库中删除 id 字段。

我使用 Mysql 作为我的数据库。请确保此问题在其他数据库中可能无法解决。

【讨论】:

    【解决方案2】:

    删除了迁移文件和最近的历史记录并修复了它。

    【讨论】:

    • 您是如何删除“最近的历史记录”的?在删除迁移文件之前使用migrate 0001migrate 0001 --fake
    • @hobs 我遇到了同样的问题。我敢打赌他是手动完成的:他打开 my_app > migrations > pycache 并删除了所有 git 未跟踪的 .pyc 文件(我敢打赌他使用的是 git 和 Atom 或 vs Studio 代码之类的编辑器),然后 my_app > migrations 并删除了所有 . py 文件未被 git 跟踪。
    【解决方案3】:

    我遇到了同样的问题,

    我在我的所有应用程序文件中注释掉了对该模型的所有引用,然后运行 ​​makemigrations 并迁移,但它一直告诉“一个模型不能有多个 AutoField”。即使我试图删除该文件。

    我不得不重置我的数据库。

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2016-02-01
      • 1970-01-01
      • 2019-02-22
      • 2014-05-24
      • 1970-01-01
      • 2012-09-29
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多