gpt4 book ai didi

python - django.db.utils.IntegrityError : duplicate key value violates unique constraint, 而 4 行中有 2 行不同

转载 作者:行者123 更新时间:2023-12-01 00:35:13 29 4
gpt4 key购买 nike

我在数据库中有一行是:

order_id:    product_type_id:    is_child:    child_type_id:

1 2 True 3

我想使用以下代码添加一行:

订单 = 1,

产品.父级 = 2

order_item = OrderItem.objects.create(order=order, product_type=product.parent, 
child_type_id=None, is_child=False)

但它会引发此错误:

django.db.utils.IntegrityError: duplicate key value violates unique constraint "order_orderitem_order_id_product_type_id_ee5c1733_uniq" DETAIL: Key (order_id, product_type_id)=(1, 2) already exists.

虽然 4 行中有 2 行不同,但为什么我会遇到这个问题?

最佳答案

Django 在数据复制方面存在问题,因此我认为不能拥有两个具有相同 id 的对象,除非它是外键或 ManyToMany 字段。

关于python - django.db.utils.IntegrityError : duplicate key value violates unique constraint, 而 4 行中有 2 行不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839889/

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