Issue with unique key error on Many to Many field

87 views
Skip to first unread message

Malibu

unread,
Nov 10, 2019, 2:41:00 PM11/10/19
to Django users
Hi there.  I've been doing Django for awhile but I am stumped on this one..  I have a many to many relationship with a 'through' definition

main.models:

class Client(models.Model):
uid = models.CharField(max_length=128, unique=True)
key = models.CharField(max_length=128)
img = models.TextField()
version = models.CharField(max_length=20)
lastConnection = models.DateTimeField()
role = models.CharField(max_length=128,default="")


tracker.models:

from main.models import Client

class PassengerAccess(models.Model):
passenger = models.ForeignKey('Passenger',on_delete=models.CASCADE)
client = models.ForeignKey(Client,on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True,blank=True)
selected = models.BooleanField(default=False)

class Passenger(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
access_code = models.CharField(max_length=32)
school_division = models.ForeignKey(SchoolDivision)
open_clients = models.ManyToManyField(Client, through='PassengerAccess')


The error I get when I try to migrate is: django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "tracker_passenger"

Any solution to this error appears to be related to a foreign key that is referencing a field that is not unique, but in this case passenger and client should both be accessing the primary key should they not? I always use plain integer primary keys.

Any suggestions on how to troubleshoot this would be appreciated.

Integr@te System

unread,
Nov 10, 2019, 5:55:06 PM11/10/19
to django...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/db29f623-eecd-4b58-8b70-d2fcd063aaaf%40googlegroups.com.

Malibu

unread,
Nov 10, 2019, 11:26:28 PM11/10/19
to Django users
Hi, thanks for the answer.  Of course, the moment I posted the question I figured out what it was.

I ended up checking my primary keys, and found that they didn't have unique constraints.  I added them and all was fine.

Must have fallen off during an import.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages