How come in Django, uniques are not holding up?
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
Is this normal? As you can see, I have title and adinfo uniqued...I did NOT want #1114 to be inserted. But it did. How do I remove all the duplicates in the database?
The way you specified your UNIQUE constraint, you're stating that you don't allow insertion of duplicates for pairs.
As you specified, you would be able to insert the pairs:
(1113, 'Tides Tavern') and (1114, 'Tides Tavern')
or
(1113, 'Roman road') and (1113, 'Tides Tavern')
but not:
(1113, 'Tides Tavern') and (1113, 'Tides Tavern')
In other words, from Postgresql docs: "A multicolumn unique index will only reject cases where all of the indexed columns are equal in two rows."
精彩评论