Talk to people,
I need to create a structure template in which all my tables have a primary key consisting of id and version, so I can have multiple versions for each id.
Problem:
CREATE TABLE table1 {
id BIGSERIAL,
version INT NOT NULL,
CONSTRAINT pk_table1 PRIMARY KEY (id, version)
}
CREATE TABLE table2 {
id BIGSERIAL,
version INT NOT NULL,
table1_id BIGINT NOT NULL,
CONSTRAINT pk_table2 PRIMARY KEY (id, version),
CONSTRAINT fk_table1 FOREIGN KEY (table1_id) REFERENCES table1(id)
}
When executing the above structure, by not referencing all fields of the composite primary key of table1
in FK, the following error is returned:
ERROR: there is no unique constraint matching given keys for referenced table "table1 "
My question is in relationship with other tables. I would like to not need to create a version field for every FK I create in the table to simplify the number of fields, be able to keep the relationship of records only by id, and track the history of the relationship by date fields. >
I've read Postgre documentation about the MATCH PARTIAL
clause I believe which would be my solution, but has not yet been implemented.
- Is there any work around for this situation?
- What am I doing against some principle of data modeling? Should I run into a different solution?
I'm using PostgreSQL version 10.5.