[英]Django - A model can't have more than one AutoField
This problem randomly started appearing for me.这个问题随机开始出现在我身上。 I know that Django generates its own ids but a lot my code has been using custom AutoFields which HAS been working.
我知道 Django 会生成自己的 id,但我的很多代码一直在使用一直在工作的自定义 AutoFields。 I added a new class today and tried to makemigrations but this error keeps showing up.
我今天添加了一个新课程并尝试进行迁移,但此错误不断出现。
I DID remove all instances of AutoFields and retried migrating but the problem still persists which leads me to believe it's something else... I don't believe my Django version has changed at all...我确实删除了 AutoFields 的所有实例并重试迁移,但问题仍然存在,这让我相信这是别的东西......我不相信我的 Django 版本根本没有改变......
Error:错误:
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.
Here's an example of one of my fields:这是我的一个领域的一个例子:
assetid = models.AutoField(primary_key=True)
A lot of my code already depends on the name itself so changing it is going to be a big issue.我的很多代码已经依赖于名称本身,因此更改它将是一个大问题。 Furthermore, this was working perfectly before!
此外,这之前工作得很好! I just can't seem to migrate it now.
我现在似乎无法迁移它。 I should mention im using a sqlite3 db.
我应该提到我使用 sqlite3 db。
This is Because Django By default uses AutoField for the id.....so, if you want other fields to be AutoField, then make sure you confirm primary_key=True .这是因为 Django 默认使用 AutoField 作为 id .....所以,如果您希望其他字段为 AutoField,请确保确认primary_key=True 。 Doing so deletes the id field from the Database.
这样做会从数据库中删除 id 字段。
I have used Mysql as my database.我使用 Mysql 作为我的数据库。 Be sure this problem might not solve in other databases.
确保此问题在其他数据库中可能无法解决。
删除了迁移文件和最近的历史记录并修复了它。
I was having the same problem,我遇到了同样的问题,
I commented out all the references to that model in all my app files, then run makemigrations and migrate, butit keeps telling that "A model can't have more than one AutoField."我在我的所有应用程序文件中注释掉了对该模型的所有引用,然后运行 makemigrations 并迁移,但它一直告诉“一个模型不能有多个 AutoField”。 even if i am trying to deleting that file.
即使我试图删除该文件。
I had to reset my database.我不得不重置我的数据库。
在我的情况下,我删除了 id Autofield ,它对我来说很好,因为 Django 默认使用 Autofield
更好的方法是取消应用迁移(意味着运行以前的迁移)。这样,有时不会从数据库中删除的字段会自动从数据库中删除。然后,在更改之后,您可以轻松地再次进行 makemigrations 然后迁移,一切都会好起来的.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.