mardi 7 juillet 2015

Self Referencing table : UNIQUE constraint failed (SQL)

Architecture

I have the following self referencing table. Every Entity has a relation with 1 to many entities.

enter image description here

Tables

Since it's a 1 to many relationship I representation Relation by a table.

CREATE TABLE ENTITY (ID TEXT PRIMARY KEY NOT NULL, VALUE TEXT);
CREATE TABLE RELATION (ID_SOURCE TEXT NOT NULL, ID_DESTINATION TEXT NOT NULL, PREDICAT TEXT, PRIMARY KEY(ID_SOURCE, ID_DESTINATION), FOREIGN KEY(ID_SOURCE) REFERENCES ENTITE(ID), FOREIGN KEY(ID_DESTINATION) REFERENCES ENTITE(ID));

Problem

When I am inserting the values I am getting the following error:

UNIQUE constraint failed: RELATION_ENTITY.ID, RELATION_ENTITY.ID_DESTINATION

I know why I am getting this error. I am inserting the same Ids in the table Relation But the value is different. THE entity m.06y3r references the entity m.02jvmvm with two different values. How can I fix this design error?

ID_SOURCE    ID_DESTINATION          VALUE
 m.06y3r       m.02jvmvm        adopted_child.adoptive
 m.06y3r       m.02jvmvm        people.person.parents

Aucun commentaire:

Enregistrer un commentaire