On Dec 15, 2009, at 17:59 , Michael Bayer wrote:

> My understanding is that INHERITS is usually used
> in practice to provide transparent "sharding" of table data and not
> necessarily to express class hierarchies, but this is strictly anecdotal
> knowledge.


Agreed.

The PostgreSQL documentation[1] mentions many caveats with INHERITS, most 
notably:

    "A serious limitation of the inheritance feature is that indexes (including 
unique constraints) and foreign key constraints only apply to single tables, 
not to their inheritance children. This is true on both the referencing and 
referenced sides of a foreign key constraint."

For those reasons it is also actively *discouraged* for expressing class 
hierarchies.

To put that into context, consider the following:

CREATE TABLE vehicle (
     id integer NOT NULL,
     owner_id integer,
     price integer,
     primary key (id) -- I added this
);

CREATE TABLE vehicle_bus (
     passengers integer
)
INHERITS (vehicle);

CREATE TABLE vehicle_whatever(
    id integer primary key references vehicle(id),
    whatever text
);

INSERT INTO vehicle_bus VALUES (1, 1, 42, 123);

SELECT * FROM vehicle;
 id | owner_id | price 
----+----------+-------
  1 |        1 |    42
(1 row)

SELECT * FROM vehicle_bus;
 id | owner_id | price | passengers 
----+----------+-------+------------
  1 |        1 |    42 |        123
(1 row)

INSERT INTO vehicle_whatever VALUES (1, 'but clearly this is in vehicle, no?');
ERROR:  insert or update on table "vehicle_whatever" violates foreign key 
constraint "vehicle_whatever_id_fkey"
DETAIL:  Key (id)=(1) is not present in table "vehicle".

~

[1] http://www.postgresql.org/docs/8.4/static/ddl-inherit.html

--
Alex Brasetvik

--

You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


Reply via email to