0

I am trying to create a 2-way relationship table in PostgreSQL for my 3 objects. This idea has stemmed from the following question https://dba.stackexchange.com/questions/48568/how-to-relate-two-rows-in-the-same-table where I also want to store the relationship and its reverse between rows.

For context on my database: Object 1 which contains (aka relates to many) object2s. In turn, these object2s also relate to many object3s. A 1-to-many relationship (object 1 to object 2) and many-to-many relationship (object 2 to object 3)

Each of the objects have been assigned a UUID in other tables which contain info regarding them. Based on their UUID's I want to be able to query them and get the associated objects UUID as well. This in turn will show me the associations and direct me as to which object I should be looking at for location, info, etc just by knowing the UUID.

PLEASE NOTE - THAT ONE BOX MAY HAVE A RELTIONSHIP OF 10 SLOTS. THEREFORE THAT ONE UUID ASSIGNED FOR THE BOX WILL APPEAR IN MY UUID1 COLUMN 10 TIMES!! THIS IS A MUST!

My next step was to try and create a directionless relationship using this query:

CREATE TABLE bridge_x 
(uuid1 UUID NOT NULL REFERENCES temp (uuid1), uuid2 UUID NOT NULL REFERENCES  temp (uuid2), 
PRIMARY KEY(uuid1, uuid2), 
CONSTRAINT  temp_temp_directionless 
FOREIGN KEY (uuid2, uuid1) 
REFERENCES bridge_x (uuid1, uuid2)
);

Is there any other way I can store ALL the information mentioned and be able to query the UUID in order to see the relationship between the objects?

User_289
  • 59
  • 1
  • 7
  • temp.uuid1 is **not** unique. – wildplasser Feb 16 '21 at 12:51
  • 1
    The question doesn't get better by [asking it twice](https://stackoverflow.com/questions/66216108/creating-a-2-way-directionless-relationship-in-one-table-in-postgresql). It is just hard to figure out what your data mean and what you are trying to do. Recommendation: describe the underlying problem you are trying to solve. – Laurenz Albe Feb 16 '21 at 12:53
  • @wildplasser is there a work around of this? As I can’t have temp.uuid1 as unique but still need a directionless relationship – User_289 Feb 16 '21 at 13:00
  • @laurenzalbe effectively because my data is all related/associated I want a table where I can query UUID1 and it will give me the associated object as well in the result – User_289 Feb 16 '21 at 13:05
  • You need *the combination* of uuid1 & uuid2 to be unique. (and you dont need uuid3 and uuid4 in the bridge table) – wildplasser Feb 16 '21 at 13:06
  • Can a slot contain more than one box(es)? – wildplasser Feb 16 '21 at 15:11

1 Answers1

1

You'll need a composite primary key in the bridge table. An example, using polygameous marriages:


CREATE TABLE person
        (person_id INTEGER NOT NULL PRIMARY KEY
        , name varchar NOT NULL
        );

CREATE TABLE marriage
        ( person1 INTEGER NOT NULL
        , person2 INTEGER NOT NULL
        , comment varchar
        , CONSTRAINT marriage_1 FOREIGN KEY (person1) REFERENCES person(person_id)
        , CONSTRAINT marriage_2 FOREIGN KEY (person2) REFERENCES person(person_id)
        , CONSTRAINT order_in_court CHECK (person1 < person2)
        , CONSTRAINT polygamy_allowed UNIQUE (person1,person2)
        );

INSERT INTO person(person_id,name) values (1,'Bob'),(2,'Alice'),(3,'Charles');

INSERT INTO marriage(person1,person2, comment) VALUES(1,2, 'Crypto marriage!') ; -- Ok
INSERT INTO marriage(person1,person2, comment) VALUES(2,1, 'Not twice!' ) ; -- Should fail
INSERT INTO marriage(person1,person2, comment) VALUES(3,3, 'No you dont...' ) ; -- Should fail
INSERT INTO marriage(person1,person2, comment) VALUES(2,3, 'OMG she did it again.' ) ; -- Should fail (does not)
INSERT INTO marriage(person1,person2, comment) VALUES(3,4, 'Non existant persons are not allowed to marry !' ) ; -- Should fail

SELECT p1.name, p2.name, m.comment
FROM marriage m
JOIN person p1 ON m.person1 = p1.person_id
JOIN person p2 ON m.person2 = p2.person_id
        ;

Result:


CREATE TABLE
CREATE TABLE
INSERT 0 3
INSERT 0 1
ERROR:  new row for relation "marriage" violates check constraint "order_in_court"
DETAIL:  Failing row contains (2, 1, Not twice!).
ERROR:  new row for relation "marriage" violates check constraint "order_in_court"
DETAIL:  Failing row contains (3, 3, No you dont...).
INSERT 0 1
ERROR:  insert or update on table "marriage" violates foreign key constraint "marriage_2"
DETAIL:  Key (person2)=(4) is not present in table "person".
 name  |  name   |        comment        
-------+---------+-----------------------
 Bob   | Alice   | Crypto marriage!
 Alice | Charles | OMG she did it again.
(2 rows)
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • Your goal is unclear. Your solution starts to look like an x/y problem. Please state your goal first, otherwise noone can answer your question. – wildplasser Feb 16 '21 at 14:15
  • Please put your explanations in the question. Also explain what uuid[1234] *mean*, and why you need them. And: what is directionless? – wildplasser Feb 16 '21 at 14:21