Last active
March 8, 2018 20:45
-
-
Save erichocean/9bc11d8c3ccfdfee6815ad61bb2cd850 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE a ( | |
id UUID NOT NULL DEFAULT gen_random_uuid(), | |
PRIMARY KEY (id) | |
); | |
CREATE TABLE aa ( | |
a UUID NOT NULL, | |
at TIMESTAMPTZ NOT NULL DEFAULT statement_timestamp(), | |
PRIMARY KEY (a, at), | |
CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES a (id) | |
) INTERLEAVE IN PARENT a (a); | |
CREATE TABLE ab ( | |
a UUID NOT NULL, | |
at TIMESTAMPTZ NOT NULL DEFAULT statement_timestamp(), | |
PRIMARY KEY (a, at), | |
CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES a (id) | |
) INTERLEAVE IN PARENT a (a); | |
CREATE TABLE ac ( | |
a UUID NOT NULL, | |
aa TIMESTAMPTZ, | |
ab TIMESTAMPTZ, | |
PRIMARY KEY (a), | |
CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES a (id), | |
CONSTRAINT fk_aa FOREIGN KEY (a, aa) REFERENCES aa (a, at), | |
CONSTRAINT fk_ab FOREIGN KEY (a, ab) REFERENCES ab (a, at) | |
) INTERLEAVE IN PARENT a (a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE | |
CREATE TABLE | |
CREATE TABLE | |
pq: column "a" cannot be used by multiple foreign key constraints | |
Error: pq: column "a" cannot be used by multiple foreign key constraints | |
Failed running "sql" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE a ( | |
id UUID NOT NULL DEFAULT gen_random_uuid(), | |
PRIMARY KEY (id) | |
); | |
CREATE TABLE aa ( | |
a UUID NOT NULL, | |
at TIMESTAMPTZ NOT NULL DEFAULT statement_timestamp(), | |
PRIMARY KEY (a, at), | |
CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES a (id) | |
) INTERLEAVE IN PARENT a (a); | |
CREATE TABLE ab ( | |
a UUID NOT NULL, | |
at TIMESTAMPTZ NOT NULL DEFAULT statement_timestamp(), | |
PRIMARY KEY (a, at), | |
CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES a (id) | |
) INTERLEAVE IN PARENT a (a); | |
CREATE TABLE ac ( | |
a UUID NOT NULL, | |
a1 UUID NOT NULL CHECK (a = a1), -- <====== duplicate 'a' here with a check() statement | |
aa TIMESTAMPTZ, | |
ab TIMESTAMPTZ, | |
PRIMARY KEY (a), | |
CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES a (id), | |
CONSTRAINT fk_aa FOREIGN KEY (a, aa) REFERENCES aa (a, at), | |
CONSTRAINT fk_ab FOREIGN KEY (a1, ab) REFERENCES ab (a, at) -- <====== use the duplicate 'a1' instead in the FK constraint | |
) INTERLEAVE IN PARENT a (a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE | |
CREATE TABLE | |
CREATE TABLE | |
CREATE TABLE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The expected result is that all rows in
ac
match:a
aa
ab
You can also delete any row in
ac
without deleting a matching row ina
,aa
, orab
, but you cannot delete a row froma
,aa
, orab
if there is a row inac
that matches (without updating or deleting the row inac
in the same transaction).