【问题标题】:Django migrations not detecting unique=True changeDjango 迁移未检测到 unique=True 更改
【发布时间】: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)

【问题讨论】:

标签: python django python-3.x postgresql


【解决方案1】:

尝试清除 hashmappings 表中的数据,然后运行 ​​migrate 命令 -> python manage.py migrate

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 2021-09-19
    • 2020-07-02
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多