2

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)
xyres
  • 20,487
  • 3
  • 56
  • 85
Ron Doe
  • 21
  • 1
  • Since adding the unique attribute, have you ran `makemigrations` & then migrated the database? – markwalker_ Jan 04 '19 at 11:28
  • Have you checked this topic https://stackoverflow.com/questions/42234330/django-error-on-migration-there-is-no-unique-constraint-matching-given-keys-fo ? – rollingthedice Jan 04 '19 at 11:40

1 Answers1

-1

try to clear data in hashmappings table and then run migrate command -> python manage.py migrate