When trying to use unique_together
on two foreign key constraints, I get the following error:
CommandError: System check identified some issues:
ERRORS:
main.AuthorTag: (models.E012) 'unique_together' refers to the non-existent field 'author'.
main.AuthorTag: (models.E012) 'unique_together' refers to the non-existent field 'tag'.
On the following table
class AuthorTag(models.Model):
class Meta:
unique_together = (('tag', 'author',),)
tag = models.ForeignKey(TagIndex, on_delete=models.CASCADE),
author = models.ForeignKey(Author, on_delete=models.CASCADE),
The same example seems to work here: Unique foreign key pairs with Django But I can't figure out why I get this error
EDIT:
changing unique_together = (('tag', 'author',),)
to unique_together = (('tag', 'author'))
give me the same error. as well as moving the meta class below the field declarations.
Removing the trailing commas, making the code to:
class AuthorTag(models.Model):
class Meta:
unique_together = ['tag', 'author']
tag = models.ForeignKey(TagIndex, on_delete=models.CASCADE)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
Made it work. I am unsure why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With