Skip to content
Advertisement

error: there is no unique constraint matching given keys for referenced table “incident”

I know that this question has been already answered a million of times, but I couldn’t find any solution. Well I have these three tables on postgres sql.

When I try to create the table incident_has_volunteer it throws the error there is no unique constraint matching given keys for referenced table "incident".

I tried to add on the third table and the patientId as a foreign key from table incident table but with no luck. I can’t understand why it throws this error even if I have already set the primary keys on the incident table.

Advertisement

Answer

I’m not an expert in postgres, but I believe that the problem is while fk_incident is referencing incident.id, incident‘s primary key is made of id + patientId. Since incident.id is guaranteed to be unique only in combination with patientId, there’s no way to ensure referential integrity.

I believe that if you add a unique constraint to incident.id (I’m assuming that it would be unique), your foreign key will be legal.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement