0
我有一个模型,它看起来像这样:南迁 - 如何添加一个唯一约束时处理数据不一致
class CollectionResource(models.Model):
resource = models.ForeignKey(Resource)
collection = models.ForeignKey(Collection)
order = models.IntegerField(null=True, blank=True)
我想确保每个集合都会有一个唯一的订单价值,所以我说:
class Meta:
unique_together = ('collection', 'order')
manage.py schemamigration --auto my_app
manage.py migrate my_app
,但我得到的完整性错误,因为在我的数据库中的值不根据新的限制 (我已经有这对奥德多次相同的值的集合)
我知道南方不会制造一些巫术魔法并修复我的数据:)但我不知道如何修复这些值。我应该为这项工作写一个原始的SQL吗?
在此先感谢
请参阅我的相关答案:http://stackoverflow.com/questions/13700200/django-remove-duplicate-objects-where-there-is-more-than-one-field-to-compare/13700642#1370064 –