Re: Foreign keys and inheritance - Mailing list pgsql-novice

From Richard Broersma Jr
Subject Re: Foreign keys and inheritance
Date
Msg-id 20060407053713.79656.qmail@web31803.mail.mud.yahoo.com
Whole thread Raw
In response to Foreign keys and inheritance  ("Pierre Thibaudeau" <pierdeux@gmail.com>)
Responses Re: Foreign keys and inheritance
List pgsql-novice
 All along, I had been under the impression that a
> tuple belonging to a child automatically belongs to the parent, yet I
> wouldn't be able to reference that tuple???

I was under the same impression also.  However, I didn't take any time to play with inherited
tables as I wanted to relate two child tables together via a relate table.  The docs explicitly
same that currently this isn't possible. (However, it is on the to-do list for 8.2 :-D. I am
really excited about that.  It should be added as long as it doesn't get pushed aside for more
important additions.)

However, if I interpret the docs correctly, could make sense that relation breaks down because the
tuple only appears to be in the parent table when you select * from parent;.

Notice the use of the Key work ONLY in the snippet of the 8.1.3 postgres document:

   In PostgreSQL, a table can inherit from zero
   or more other tables, and a query can reference
   either all rows of a table or all rows of a
   table plus all of its descendant tables. The
   latter behavior is the default. For example,
   the following query finds the names of all
   cities, including state capitals, that are
   located at an altitude over 500ft:

  SELECT name, altitude
      FROM cities
      WHERE altitude > 500;
   Given the sample data from the PostgreSQL
   tutorial (see Section 2.1),   this returns:

     name    | altitude
  -----------+----------
   Las Vegas |     2174
   Mariposa  |     1953
   Madison   |      845
   On the other hand, the following query finds
   all the cities that are not state capitals
   and are situated at an altitude over 500ft:

  SELECT name, altitude
      FROM ONLY cities
      WHERE altitude > 500;

     name    | altitude
  -----------+----------
   Las Vegas |     2174
   Mariposa  |     1953
  Here the ONLY keyword indicates that the query
  should apply only to cities, and not any tables
  below cities in the inheritance hierarchy. Many
  of the commands that we have already discussed
  — SELECT, UPDATE and DELETE — support the ONLY
  keyword.



If I was to take a guess why the relation breaks,  It would be that the table inheritance really
only works like a union query on two tables.  So in reality, the parent table may not really have
the child's tuple in it.


>
> Is that normal behaviour?  Can I relax the foreign key constraint so as to
> be able to reference any item within "generic", whether the tuple originally
> came from "generic" or from one of its children?
>
> (Running PostgreSQL v.8.1.3 on WindowsXP.)
>

I was able to emulate inherited tables using one parent table and two child tables. The child
table linked to the parent table with unique foriforeigns.  To simplify the insertion and
selection of the two child and parent tables it made use of two insert-able, update-able,
delete-able views.

This gives me full control and allows me to create relations on any of the tables.  The down side
is that this schema is labor intensive.

Regards,

Richard Broersma


pgsql-novice by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: Scheduled Job at PostgreSQL
Next
From: Richard Broersma Jr
Date:
Subject: Re: Scheduled Job at PostgreSQL