gpt4 book ai didi

python - 为什么在 Django 中,uniques 不成立?

转载 作者:行者123 更新时间:2023-11-29 00:54:41 25 4
gpt4 key购买 nike

class A(models.Model):
title = models.CharField(max_length=240,null=True, blank=True, db_index=True)
body = models.TextField(blank=True, null=True)
adinfo = models.CharField(max_length=240, null=True, blank=True, db_index=True)
url = models.CharField(max_length=10000, null=True,blank=True)
img = models.CharField(max_length=10000, null=True,blank=True)
created_at = models.DateTimeField(auto_now_add=True, null=True, db_index=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
class Meta:
unique_together = (('title','adinfo'),)


mysql> select * from mo_a where id = 1113\G;
*************************** 1. row ***************************
id: 1113
title: Tides Tavern
body: Come in and enjoy the morning sun or a nice sunset with breakfast, lunch or dinner. Find a seat, put your feet up & enjoy. Click here!
adinfo: NULL
url:
img: http://creative.ak.fbcdn.net/v41818/flyers/125/47/13039135731061564765_1_89254352.jpg
created_at: 2011-07-08 00:41:18
updated_at: 2011-07-08 00:41:18
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> select * from mo_a where id = 1114\G;
*************************** 1. row ***************************
id: 1114
title: Tides Tavern
body: Come in and enjoy the morning sun or a nice sunset with breakfast, lunch or dinner. Find a seat, put your feet up & enjoy. Click here!
adinfo: NULL
url:
img: http://creative.ak.fbcdn.net/v41818/flyers/125/47/13039135731061564765_1_89254352.jpg
created_at: 2011-07-08 00:41:22
updated_at: 2011-07-08 00:41:22
1 row in set (0.00 sec)

ERROR:
No query specified

这正常吗?如您所见,我有唯一的标题和广告信息...我不想插入#1114。但确实如此。如何删除数据库中的所有重复项?

最佳答案

您指定 UNIQUE 约束的方式是在声明您不允许为对插入重复项。

正如您指定的那样,您将能够插入对:

(1113, 'Tides Tavern') and (1114, 'Tides Tavern')

(1113, 'Roman road') and (1113, 'Tides Tavern')

但不是:

(1113, 'Tides Tavern') and (1113, 'Tides Tavern')

换句话说,来自 Postgresql 文档:“多列唯一索引只会拒绝两行中所有索引列都相等的情况。”

关于python - 为什么在 Django 中,uniques 不成立?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6678314/

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