Creating foreign key constraint to child table? - Mailing list pgsql-sql

From Allan Engelhardt
Subject Creating foreign key constraint to child table?
Date
Msg-id 3B6D40D2.BDF7B876@cybaea.com
Whole thread Raw
Responses Re: Creating foreign key constraint to child table?
Re: Creating foreign key constraint to child table?
List pgsql-sql
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
keysfor 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
TABLEwill 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,
justto get the tableoid for bar ?  There *is* an index on pg_class.relname but still...
 

   --- Allan.



pgsql-sql by date:

Previous
From: Allan Engelhardt
Date:
Subject: Using functions in SQL statements
Next
From: Allan Engelhardt
Date:
Subject: Re: Creating foreign key constraint to child table?