foreign keys with inherited tables

Started by Matt Magoffinalmost 25 years ago2 messagesgeneral
Jump to latest
#1Matt Magoffin
mmagoffin@proxicom.com

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

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Matt Magoffin (#1)
Re: foreign keys with inherited tables

On Mon, 16 Apr 2001, Matt Magoffin wrote:

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?

Not currently in that form using the foreign key constraints. Hopefully
for 7.2.

I believe a work around using a separate key table was post about a month
ago or so.