I am trying to use a double primary key as a foreign key.
Create table AAA (
AAA_id int primary key
);
create table BBB (
AAA_id int,
BBB_name character varying(20),
primary key (AAA_id, BBB_name)
);
create table CCC (
AAA_id,
BBB_name,
DDD_id,
... ???
);
table AAA is an object
table BBB is many to one with AAA, and holds aliases of AAA
I am trying to create a pivot table, CCC which holds a many to one between DDD and BBB.
I guess I want something like
create table CCC (
AAA_id,
BBB_name,
DDD_id,
foreign key (AAA_id, BBB_name) references BBB(AAA_id, BBB_name) on update cascade
);
where both AAA_id and BBB_name are foreign keys, but they are also always referring to the same row in BBB.
but of course that's not valid. what is the best way to produce this type of behavior in postgreSQL?