Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f75DWG672016 for ; Sun, 5 Aug 2001 09:32:16 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f75D32U72385 for pgsql-sql@postgresql.org; Sun, 5 Aug 2001 09:03:02 -0400 (EDT) (envelope-from news) From: Allan Engelhardt X-Newsgroups: comp.databases.postgresql.novice,comp.databases.postgresql.sql Subject: Re: Creating foreign key constraint to child table? Date: Sun, 05 Aug 2001 14:03:05 +0100 Organization: http://cybaea.com/ Message-ID: <3B6D4409.64B17684@cybaea.com> References: <3B6D40D2.BDF7B876@cybaea.com> X-NNTP-Posting-Host: engelhardt.demon.co.uk:193.237.12.86 X-Complaints-To: abuse@demon.net X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.7 i686) X-Accept-Language: en MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 43 To: pgsql-sql@postgresql.org X-Archive-Number: 200108/42 X-Sequence-Number: 4263 I obviously haven't had enough coffee yet... :-) The following script works as expected. drop database test; create database test; \c test create table foo (id integer primary key); create table bar () inherits (foo); create unique index bar_id_idx ON bar(id); create table baz (bar integer, constraint fk_bar foreign key (bar) references bar(id)); insert into foo values (1); insert into bar values (2); insert into baz values (2); insert into baz values (1); -- fails Sorry. --- Allan. I wrote: > 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... > > --- Allan.