evrom
evrom

Reputation: 93

'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.

Upvotes: 6

Views: 8436

Answers (1)

evrom
evrom

Reputation: 93

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.

Upvotes: 2

Related Questions