Skip to content

Instantly share code, notes, and snippets.

@ppadron
Last active December 26, 2015 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppadron/7095522 to your computer and use it in GitHub Desktop.
Save ppadron/7095522 to your computer and use it in GitHub Desktop.
"A serious limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single tables, not to their inheritance children. This is true on both the referencing and referenced sides of a foreign key constraint." http://www.postgresql.org/docs/9.3/static/ddl-inherit.html#DDL-INHERIT-…
CREATE TABLE people (
id serial primary key,
name text
);
CREATE TABLE scientists (
university text
) INHERITS (people);
CREATE TABLE chefs (
hat_size text
) INHERITS (people);
CREATE TABLE people_i_like (
person_id integer references people(id),
reason text
);
insert into scientists values (default, 'Alice', 'MIT');
insert into chefs values (default, 'Bob', 'medium');
insert into people_i_like values (1, 'taught me math');
--ERROR: insert or update on table "people_i_like" violates foreign key constraint "people_i_like_people_id_fkey"
--DETAIL: Key (person_id)=(1) is not present in table "people".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment