Postgres FK referencing composite PK

Postgresql

Postgresql Problem Overview


Consider

CREATE TABLE foo (
	id SERIAL,
	foo_created_on ABSTIME,
	foo_deactivated_on ABSTIME,
	PRIMARY KEY (id, foo_created_on)
);

CREATE TABLE bar (
	id SERIAL,
	bar_created_on ABSTIME,
	bar_deactivated_on ABSTIME,
	foo_id REFERENCES ( .. what goes here? ..),
	PRIMARY KEY (id, bar_created_on)
);

How do I create an FK in "bar" that references the PK in "foo"?

Postgresql Solutions


Solution 1 - Postgresql

> How do I create an FK in "bar" that references the PK in "foo"?

With your current structure, you can't.

The target of a foreign key reference has to be declared either PRIMARY KEY or UNIQUE. So either this

CREATE TABLE foo (
    id SERIAL PRIMARY KEY,
    foo_created_on ABSTIME,
    foo_deactivated_on ABSTIME,
    UNIQUE (id, foo_created_on)
);

or this

CREATE TABLE foo (
    id SERIAL,
    foo_created_on ABSTIME,
    foo_deactivated_on ABSTIME,
    PRIMARY KEY (id, foo_created_on),
    UNIQUE (id)
);

would work as a target for bar.foo_id. Then bar would have a simple reference.

CREATE TABLE bar (
    id SERIAL,
    bar_created_on ABSTIME,
    bar_deactivated_on ABSTIME,
    foo_id REFERENCES foo (id),
    PRIMARY KEY (id, bar_created_on)
);

If you want to reference the primary key you originally declared in foo, you have to store that primary key in bar. You have to store all of it, not part of it. So without modifying foo, you could build bar like this.

CREATE TABLE bar (
    id SERIAL,
    bar_created_on ABSTIME,
    bar_deactivated_on ABSTIME,
    foo_id INTEGER NOT NULL,
    foo_created_on ABSTIME NOT NULL,
    FOREIGN KEY (foo_id, foo_created_on) REFERENCES foo (id, foo_created_on),
    PRIMARY KEY (id, bar_created_on)
);

Solution 2 - Postgresql

You have to create separate foreign keys:

CREATE TABLE bar (
  id SERIAL,
  bar_created_on ABSTIME,
  bar_deactivated_on ABSTIME,
  foo_id INT,
  FOREIGN KEY (foo_id, created_on) REFERENCES foo (id, created_on),
  PRIMARY KEY (id, bar_created_on)
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpunkishView Question on Stackoverflow
Solution 1 - PostgresqlMike Sherrill 'Cat Recall'View Answer on Stackoverflow
Solution 2 - PostgresqlDave HalterView Answer on Stackoverflow