0

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

2 Answers 2

3

It is all because in Relation table you have PRIMARY KEY(ID_SOURCE, ID_DESTINATION) so this both values have to create unique set. If you change it to PRIMARY KEY(ID_SOURCE, ID_DESTINATION, VALUE), then it should work just fine.

EDIT

example of solution mentioned in comment below:

CREATE TABLE RELATION (
    ID INT NOT NULL,
    ID_SOURCE TEXT NOT NULL,
    ID_DESTINATION TEXT NOT NULL,
    VALUE TEXT,

    PRIMARY KEY(ID), 
    UNIQUE (ID_SOURCE, ID_DESTINATION, VALUE)
    FOREIGN KEY(ID_SOURCE) REFERENCES ENTITE(ID),
    FOREIGN KEY(ID_DESTINATION) REFERENCES ENTITE(ID));
3
  • 1
    well it is quite common, however other solution is to create PRIMARY KEY (ID) in table Relation and add UNIQUE CONSTRAINTS on set (ID_SOURCE, ID_DESTINATION, VALUE) with in my opinion is better solution.
    – T.G
    Jul 7, 2015 at 10:50
  • one last question please adding a Primaryt key to table Relation. Wont that be an error because it's a 1 to many relation. So relation is playing the role of a **Junction table **.
    – Hani Goc
    Jul 7, 2015 at 10:53
  • it is a bit offroad, however i've seen this quite a lot.
    – T.G
    Jul 7, 2015 at 10:58
1

If you want rows with duplicate values for id_source and id_destination you can either use a surrogate primary key (like an autonumber int), maybe id_relation, together with a unique constraint for the three other columns or extend the primary key to include the value column too so that it would be PRIMARY KEY(ID_SOURCE, ID_DESTINATION, VALUE).

If you want to reference this table in joins, using a surrogate key might make your life easier.

3
  • So @jpw Relation will become a table right? I mean there will be a: Entity 1 ---> M Relation then Relation 1 ---> M Entity and Of course i'll add an integer ID KEY to relation
    – Hani Goc
    Jul 7, 2015 at 10:50
  • 1
    Not sure I understand what you mean. The only changes I propose is to add the id column and a unique constraint for the three remaining columns.
    – jpw
    Jul 7, 2015 at 10:53
  • even If table Relation is a junction table right? Sorry asking these question just to be sure.
    – Hani Goc
    Jul 7, 2015 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.