具有to_feild约束的外键字段给出重复键错误

时间:2019-01-10 17:17:36

标签: django database django-models django-views relational-database

我对另一个具有to_field约束且具有指向模型中字段的点的模型有一个外键,但是当我尝试运行migration时,会出现以下错误: django.db.utils.IntegrityError:无法创建唯一索引“ disctest_respondent_least_disc_ce7da5e0_uniq” 详细信息:键(least_disc)=(4,9,1,4,6)已重复。

这是模型:

class Combined(BaseRespondent):
    least_disc = models.ForeignKey(Respondent, to_field='least_disc',
                                   related_name="combined_least_disc", on_delete=models.CASCADE)
    most_disc = models.ForeignKey(Respondent, to_field='most_disc',
                                  related_name="combined_most_disc", on_delete=models.CASCADE)

这是外键的参考模型

class Respondent(BaseRespondent):
    least_disc = models.CharField(max_length=256, default='6,0,7,7,4' ,unique=True)
    most_disc = models.CharField(max_length=256, default='7,5,1,4,7', unique=True)
    most_personality = models.ForeignKey(Personality, on_delete=models.SET_NULL,
                                         null=True, related_name="most_personality")
    least_personality = models.ForeignKey(Personality, on_delete=models.SET_NULL,
                                          null=True, related_name="least_personality")

这也是我得到的错误:

django.db.utils.IntegrityError: could not create unique index "disctest_respondent_least_disc_ce7da5e0_uniq"
DETAIL:  Key (least_disc)=(4,9,1,4,6) is duplicated.

我该如何解决这个问题?预先感谢您的及时答复。

0 个答案:

没有答案