foreign keys with inherited tables

From: Matt Magoffin <mmagoffin(at)proxicom(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: foreign keys with inherited tables
Date: 2001-04-16 19:41:59
Message-ID: 3ADB4B07.D41A744D@proxicom.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)proxicom(dot)com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Masto 2001-04-16 20:20:44 Re: Re: erServer beta
Previous Message Bob McCormick 2001-04-16 19:36:06 Schema Import