gpt4 book ai didi

python - Django : get_or_create Raises duplicate entry with together_unique

转载 作者:IT老高 更新时间:2023-10-28 22:12:41 25 4
gpt4 key购买 nike

模型示例

class Example(Stat):
numeric = models.IntegerField(...)
date = models.DateField( auto_now_add=True,...) #auto_now_add=True was the problem

class Meta:
unique_together = ('numeric','date')

)

如果 72 和 '2011-08-07' 已存储

Example.object.get_or_create(numeric=72,date='2011-08-07')

提高

django.db.utils.IntegrityError: (1062, "Duplicate entry '72-2011-08-07'

问题是为什么 get_or_create 会引发 IntegrityError,这就是使用的想法get_or_create.

不确定这是否是一个错误,我打开了一张票 https://code.djangoproject.com/ticket/16587

最佳答案

您的问题似乎是您的 get_or_create 中没有包含更多列,请参阅即 this thread在 Django 邮件列表中。

您需要使用 get_or_createdefaults 参数,如 docs 中所述。 ,或为所有列指定值,以便 get_or_create 正确匹配。

关于python - Django : get_or_create Raises duplicate entry with together_unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6974463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com