Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'unique_together' refers to the non-existent field

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.

like image 561
evrom Avatar asked Nov 01 '22 16:11

evrom


1 Answers

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.

like image 142
evrom Avatar answered Nov 10 '22 06:11

evrom