foreign keys with inherited tables - Mailing list pgsql-general

From Matt Magoffin
Subject foreign keys with inherited tables
Date
Msg-id 3ADB4B07.D41A744D@proxicom.com
Whole thread Raw
Responses Re: foreign keys with inherited tables
List pgsql-general
I'm trying to use the capabilities of foreign keys to constrain some
tables that use inheritance, but the foreign keys don't work on any
inherited (child) table but the declared (parent) table. Here's the
example:

CREATE TABLE foo (
    "id" int4,
    "name" text,
    PRIMARY KEY ("id")
);

CREATE TABLE bar (
    "blah" text,
    PRIMARY KEY ("id")
) INHERITS (foo);

CREATE TABLE foobar (
    "bar_id" int4,
    "foo_id" int4,
    CONSTRAINT bar_id_constraint
        FOREIGN KEY ("bar_id") REFERENCES bar,
    CONSTRAINT foo_id_constraint
        FOREIGN KEY ("foo_id") REFERENCES foo
        ON DELETE CASCADE
);

INSERT INTO foo VALUES (1,'foo one');
INSERT INTO bar VALUES (2,'bar one');
INSERT INTO foobar VALUES (2,1);
INSERT INTO foobar VALUES (2,2);

This results in this error on the last INSERT statement:

ERROR:  foo_id_constraint referential integrity violation - key
referenced from foobar not found in foo

which I assume is because the foo(id = 2) object is actually in the bar
table? Is there another way to get this foreign check to work?

-- m@

--
: Matt Magoffin
: mmagoffin@proxicom.com




pgsql-general by date:

Previous
From: "Dr. Evil"
Date:
Subject: Transactions inside of pl/pgsql?
Next
From: Stephan Szabo
Date:
Subject: Re: foreign keys with inherited tables