Re: Table inheritance foreign key problem

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>
Cc: achambers(at)mcna(dot)net, "Postgresql (General)" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Table inheritance foreign key problem
Date: 2010-12-22 14:37:20
Message-ID: 4526.1293028640@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Richard Broersma <richard(dot)broersma(at)gmail(dot)com> writes:
> On Tue, Dec 21, 2010 at 9:32 PM, Andy Chambers <achambers(at)mcna(dot)net> wrote:
>> create table guidebooks (
>> city check (city in (select name
>> from cities)),

> This is a nice idea. They only problem is that PostggreSQL doesn't
> support sub-selects in a tables check constraints:
> http://www.postgresql.org/docs/9.0/interactive/sql-createtable.html

And, before anybody says "what if I hide the sub-select in a function",
here's the *real* problem with trying to use a CHECK constraint as a
substitute for a foreign key: it's not checked at the right times.
CHECK is assumed to be a condition involving only the values of the row
itself, so it's only checked during insert or update. There is nothing
preventing a change in the other table from invalidating your FK
reference.

There are some subsidiary problems, like dump/reload not realizing that
there's any ordering constraint on how it restores the two tables, but
the lack of a defense against deletions in the PK table is the real
killer for this idea.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kelly Burkhart 2010-12-22 15:07:45 Re: libpq sendQuery -- getResult not returning until all queries complete
Previous Message Filip Rembiałkowski 2010-12-22 14:04:02 Re: Constraining overlapping date ranges