Received: from ajax2.sovam.com (ajax2.sovam.com [194.67.1.173]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f75LTi690620 for ; Sun, 5 Aug 2001 17:29:44 -0400 (EDT) (envelope-from dmitry@taurussoft.org) Received: from pm14-a32.dial.sovam.com ([195.218.132.32]:1353 "HELO taurussoft.org" ident: "TIMEDOUT2" whoson: "tttt@online.ru" smtp-auth: TLS-CIPHER: TLS-PEER: ) by ajax2.sovam.com with SMTP id ; Mon, 6 Aug 2001 01:29:44 +0400 Received: (qmail 2468 invoked from network); 5 Aug 2001 21:27:50 -0000 Received: from flame-in-night.taurussoft.org (192.168.107.1) by kitezh.taurussoft.org with SMTP; 5 Aug 2001 21:27:50 -0000 Received: (qmail 28909 invoked from network); 5 Aug 2001 21:28:02 -0000 Received: from localhost (127.0.0.1) by localhost with SMTP; 5 Aug 2001 21:28:02 -0000 Subject: Re: Creating foreign key constraint to child table? From: "Dmitry G. Mastrukov" =?koi8-r?Q?=E4=CD=C9=D4=D2=C9=CA_?= =?koi8-r?Q?=E7=C5=CE=CE=C1=C4=D8=C5=D7=C9=DE_?= =?koi8-r?Q?=ED=C1=D3=D4=D2=C0=CB=CF=D7?= To: Allan Engelhardt Cc: pgsql-sql@postgresql.org In-Reply-To: <3B6D40D2.BDF7B876@cybaea.com> References: <3B6D40D2.BDF7B876@cybaea.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Evolution/0.12 (Preview Release) Date: 06 Aug 2001 01:28:02 +0400 Message-Id: <997046882.2058.14.camel@flame-in-night> Mime-Version: 1.0 X-Archive-Number: 200108/45 X-Sequence-Number: 4266 05 Aug 2001 13:49:22 +0100, Allan Engelhardt ____ > I would like to create a FOREIGN KEY constraint to an inherited column, like: > > test=# CREATE TABLE foo(id INTEGER PRIMARY KEY); > test=# CREATE TABLE bar() INHERITS (foo); > test=# CREATE TABLE baz (bar INTEGER, CONSTRAINT fk_bar FOREIGN KEY (bar) REFERENCES bar(id)); > ERROR: UNIQUE constraint matching given keys for referenced table "bar" not found > > This obvioulsy doesn't work. I *can* create a FOREIGN KEY contraint to the parent table: > > test=# create table baz(bar integer, constraint fk_bar foreign key (bar) references foo(id)); > NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) > CREATE > > but this is not exactly what I want: I need to ensure that baz.bar is a bar and not just any foo. > > Do I need to write my own INSERT/UPDATE triggers on baz to check the tableoid, or is there a nice way to do this? > > Any examples on how to do this? In particular, do I need to do a SELECT on pg_class for every INSERT / UPDATE in baz, just to get the tableoid for bar ? There *is* an index on pg_class.relname but still... > Now child table does not inherit constraints from parent. More of that, you can violate primary key in parent by inserting duplicate id in child. So inheritance is broken in current version. You should develop scheme without inheritance. Regards, Dmitry