0

I created an extension of the standard user model in models.py called Client. I added a manytomany relationship to it to indicate which clients are related to another:

class Client(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    clients = models.ManyToManyField("self", blank=True)

This works perfectly fine in my local dev environment where I use python manage.py runserver. However, when I try to run python manage.py migrate on our testing server using a postgresql database, I get the following error:

psycopg2.errors.InvalidForeignKey: there is no unique constraint matching given keys for referenced table "content_client"

What am I missing here?

  • just try be deleting migrations files and run makemigration and migrate again. – Smit Parmar Jul 07 '22 at 13:19
  • This didn't work unfortunately.. – Jord Vink Jul 07 '22 at 14:11
  • This is an explanation in terms of the SQL behind it https://stackoverflow.com/questions/11966420/error-there-is-no-unique-constraint-matching-given-keys-for-referenced-table-b The question is missing code in order for us to give an answer. If you dont need your current data in the database, try dropping the database and migrate again. (a migration mis-match is my best guess) – Sorin Burghiu Jul 07 '22 at 15:12

0 Answers0