A foreign key constraint can span multiple columns. You could just add the column revision
to the table post_state
.
CREATE TEMP TABLE post (
post_id serial NOT NULL
,revision integer NOT NULL DEFAULT 0
,summary text NOT NULL
,description text NOT NULL
,user_id integer NOT NULL
REFERENCES user (id) MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT
,post_type_id integer NOT NULL
REFERENCES post_type (id) MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT
,ctime timestamptz DEFAULT NOW()
,PRIMARY KEY(post_id, revision)
);
CREATE TEMP TABLE post_state (
post_id integer NOT NULL
,revision integer NOT NULL
,assembly_seat_id integer NOT NULL
REFERENCES assembly_seat (id) MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT
,PRIMARY KEY(post_id, revision)
,FOREIGN KEY (post_id, revision) REFERENCES post (post_id, revision)
);
Read the manual about foreign key constraints.
I am using the name post_id
for the primary key column of table post
. Don't use id
as column name. If you join a bunch of tables you end up with a bunch of columns all names id
. Regrettably, some half-wit ORMs are in the habit of doing that.
Alternatively, it might be better design to have unique post_id
in table post
and add a table post_revision
with a n:1 relation to post
.