Thread: [pgsql] inherits and foreign key problem

[pgsql] inherits and foreign key problem

From
"Magicloud Magiclouds"
Date:
Dear all,
    I have some tables like below, and now i have one record for each
in simple_texts, simple_directories and side_types. Now I want to
insert one record into sides (from simple_directory to simple_text
with side_type), but it report error: can not insert or update
"sides", against foreign key "sides_from_fkey". detail: key (from)=(2)
is not present in table "base_nodes".
    How does this happen? How to resolve?

Thanks.
---
CREATE TABLE base_nodes
(
  id bigserial NOT NULL UNIQUE
);
CREATE TABLE name_descriptions
(
  name varchar (256) NOT NULL PRIMARY KEY,
  description text NOT NULL,
  UNIQUE (id)
) INHERITS (base_nodes);
CREATE TABLE side_types
(
  UNIQUE (id),
  PRIMARY KEY (name)
) INHERITS (name_descriptions);
CREATE TABLE sides
(
  "from" int8 NOT NULL REFERENCES base_nodes (id) ON DELETE CASCADE,
  "to" int8 NOT NULL REFERENCES base_nodes (id) ON DELETE CASCADE,
  "type" int8 NOT NULL REFERENCES side_types (id) ON DELETE CASCADE,
  PRIMARY KEY ("from", "to", "type")
) INHERITS (base_nodes);
CREATE TABLE users
(
  "password" char (128) NOT NULL,
  UNIQUE (id),
  PRIMARY KEY (name)
) INHERITS (name_descriptions);
CREATE TABLE simple_texts
(
  context text NOT NULL,
  UNIQUE (id),
  PRIMARY KEY (name)
) INHERITS (name_descriptions);
CREATE TABLE simple_directories
(
  UNIQUE (id),
  PRIMARY KEY (name)
) INHERITS (name_descriptions);

Re: [pgsql] inherits and foreign key problem

From
"A. Kretschmer"
Date:
am  Tue, dem 24.04.2007, um 10:30:15 +0800 mailte Magicloud Magiclouds folgendes:
> Dear all,
>    I have some tables like below, and now i have one record for each
> in simple_texts, simple_directories and side_types. Now I want to
> insert one record into sides (from simple_directory to simple_text
> with side_type), but it report error: can not insert or update
> "sides", against foreign key "sides_from_fkey". detail: key (from)=(2)
> is not present in table "base_nodes".
>    How does this happen? How to resolve?

Quotation from
http://www.postgresql.org/docs/current/interactive/ddl-inherit.html :

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.


Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net