Answer the question
In order to leave comments, you need to log in
How to make foreign key in child table before multiple parent ones?
I have tables users and teams like this:
CREATE TABLE users(
"guid" uuid NOT NULL DEFAULT uuid_generate_v4(),
"username" varchar(60) NOT NULL
);
CREATE TABLE teams (
"guid" uuid NOT NULL DEFAULT uuid_generate_v4(),
"name" varchar(60) NOT NULL
);
CREATE TABLE participants (
"contest" uuid NOT NULL,
"participant" uuid NOT NULL,
);
Answer the question
In order to leave comments, you need to log in
In the sense that participants.contest checks that such a uuid is in users.guid or teams.guid?
No, that's not possible.
Make a trigger that will copy the FK logic.
Move common fields to a separate table, merge as 1:1 with teams or users
Move common fields to a separate table, use table inheritance (inherits).
That's it teams and users are different entities and you can't mix them. Therefore, it is necessary to make two foreign keys in the participants table. Separately for users and separately for teams.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question