简体   繁体   中英

Django migrations not detecting unique=True change

I am getting the following error after trying to add a foreignkey from CrackingJob.hash_mode_numeric to HashMappings .

Initially i was trying to set the FK directly to HashMappings.hash_mode_numeric without the unique constraint and it rightly gave the error, but after adding unique=True i still get the error. Even when i try to just use the PK (auto generated unique id) as FK, like in the code below.

django.db.utils.ProgrammingError: there is no unique constraint
matching given keys for referenced table "appname_hashmappings"

Relevant code:

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM