Following is my use case:
I have 4 tables:
CREATE TABLE A
(
name character(20) NOT NULL,
id integer NOT NULL,
CONSTRAINT a_pkey PRIMARY KEY (id)
)
CREATE TABLE B
(
name character(20) NOT NULL,
id integer NOT NULL,
CONSTRAINT b_pkey PRIMARY KEY (id)
)
CREATE TABLE C
(
name character(20) NOT NULL,
id integer NOT NULL,
CONSTRAINT c_pkey PRIMARY KEY (id)
)
CREATE TABLE X
(
type character(20) NOT NULL,
other_id integer NOT NULL,
id integer NOT NULL,
CONSTRAINT "X_PK" PRIMARY KEY (id)
)
the "other_id" in the table X can be the "id" of any of the table A, B or C. The "type" column in table X is suppose to be indicating which of the tables A,B or C "id" is stored in in "other_id"
example data from table x:
type other_id id
"A" 1 1
"B" 1 2
"C" 1 3
"A" 2 4
Trying to create composite FKs between table X-A, X-B and X-C as follows using a fixed value
ALTER TABLE x
ADD CONSTRAINT X_A_FK FOREIGN KEY (other_id, type) REFERENCES a (id, 'A') ON DELETE CASCADE
but I get this error (same problem for all the FKs):
ERROR: syntax error at or near "'A'"
My question is, with a foreign composite key, it possible to reference a column and a fixed value? If not What would be a better way of approaching this problem?