【发布时间】:2019-05-30 23:13:44
【问题描述】:
尝试将外键从 CrackingJob.hash_mode_numeric 添加到 HashMappings 后,我收到以下错误。
最初我试图在没有唯一约束的情况下将 FK 直接设置为 HashMappings.hash_mode_numeric,它正确地给出了错误,但在添加 unique=True 之后我仍然得到错误。即使我尝试仅使用 PK(自动生成的唯一 ID)作为 FK,如下面的代码所示。
django.db.utils.ProgrammingError: there is no unique constraint
matching given keys for referenced table "appname_hashmappings"
相关代码:
class HashMappings(models.Model):
hash_name = models.CharField(max_length=255, unique=True)
hash_mode_numeric = models.IntegerField(unique=True)
example_hash = models.TextField(max_length=2500)
supported = models.BooleanField(default=0)
class Meta:
ordering = ['hash_name']
def __str__(self):
return f'{self.hash_name}'
class CrackingJob(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL)
description = models.CharField(max_length=255)
hash_mode_numeric = models.ForeignKey(HashMappings, on_delete=models.CASCADE)
【问题讨论】:
-
添加唯一属性后,你有没有跑
makemigrations&然后迁移数据库? -
你检查过这个话题stackoverflow.com/questions/42234330/…吗?
标签: python django python-3.x postgresql