Re: references constraint on inherited tables?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Douglas Bates <bates(at)stat(dot)wisc(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: references constraint on inherited tables?
Date: 2001-07-13 21:07:00
Message-ID: Pine.BSF.4.21.0107131357110.11325-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Yes, references constraints reference only the named table (if you look at
the triggers, they use ONLY). There's a lot of reasons for this,
partially a matter of making sure the triggers inherit properly, partially
making sure all the locking is right, and partially other things that
are wierd across inheritance trees (for example, nothing will prevent
you from updating an article to have the same id as a book, while you'd
expect that the primary key on title would have that effect).

One workaround that was come up with was using an id table and having
the master ids there and the inheritance tree reference those. Then
anything that wants to reference by id and doesn't care what type it
is references the id table.

On 13 Jul 2001, Douglas Bates wrote:

> I am using PostgreSQL 7.1.1 on a Debian GNU/Linux 3.0 (testing)
> system.
>
> In a table definition I used a REFERENCES constraint to a table that
> is inherited, thinking that this would ensure that a value for that
> column occurred in the table that I named or any of its daughter
> tables. I was wrong. Apparently the constraint checks only the
> table and not any daughter tables.
>
> If I expressed this in terms of SELECT the distinction would be like
> that between 'table' and 'table*' (pre-7.1) or between 'ONLY table'
> and 'table' (7.1 and later).
>
> Some details on the application may help. This is a bibliographic
> database storing information on books, journal articles, proceedings
> articles, book reviews, etc. We refer to any of these as "titles".
> Keywords or phrases are stored in the phrase table. Phrases and
> titles are related through the keywords table.
>
> Some of the table definitions are:
>
> -- All publications must be listed in the title table.
>
> CREATE TABLE title ( -- a virtual table - do not insert into this
> idT SERIAL PRIMARY KEY,
> type CHAR(1) CHECK(upper(type) IN ('B', 'E', 'J', 'P', 'R')),
> title TEXT NOT NULL
> );
>
> -- Information on a journal article is in the article table
>
> CREATE TABLE article (
> idE INTEGER REFERENCES jour_vol ON UPDATE CASCADE,
> bpg VARCHAR(5) ,
> epg VARCHAR(5) ,
> PRIMARY KEY (idT)
> ) INHERITS (title);
>
> -- Book_cont describes the contents of a book
>
> CREATE TABLE book_cont (
> idE INTEGER NOT NULL REFERENCES book,
> PRIMARY KEY (idT)
> ) INHERITS (title);

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2001-07-13 21:13:10 Re: [HACKERS] Re: [PATCH] To remove EXTEND INDEX
Previous Message Bruce Momjian 2001-07-13 21:00:22 Re: [PATCH] To remove EXTEND INDEX