Обсуждение: how to inherits the references...

Поиск
Список
Период
Сортировка

how to inherits the references...

От
frederik nietzsche
Дата:
hi all,
I've created some table with the inharitance,
something like:

CREATE TABLE sigles(sigle    varchar(255) PRIMARY KEY
);
 CREATE TABLE cars(       UNIQUE (sigle) )INHERITS (sigles);
   CREATE TABLE used_cars(         old_owner text,         km        text,         model     text,         year
text,        PRIMARY KEY (sigle)   )INHERITS (cars);
 
   CREATE TABLE new_cars(         model     text,         type      text,         some_other  text,         PRIMARY KEY
(sigle)  )INHERITS (cars);
 

then I have a table of owner (with some fields) that
does not inherits nothing.

and then I have a table for the relation between
used_cars and old_owner:

CREATE TABLE cars_owner_relations(     car    text   REFERENCES used_cars (sigle),     owner  text   REFERENCES
old_owner(id)
 
);

now, when I insert used_cars it also create a sigle
inside the "sigles" table, and this is OK, but when I
insert a record inside the cars_owner_relations it
says: 

ERROR:  <unnamed> referential integrity violation -
key referenced from cars_owner_relations not found in
sigles


as if the sigles where not in the "sigles" table, but
it's there!
it's probably because of the way in which psql threatsthe inheritance.
my question is (finally): is there some workaround for
this?? or: am I making some mistakes??

ok, thanks and sorry for my english...
danilo

______________________________________________________________________
Scarica il nuovo Yahoo! Messenger: con webcam, nuove faccine e tante altre novità.
http://it.yahoo.com/mail_it/foot/?http://it.messenger.yahoo.com/


Re: how to inherits the references...

От
Stephan Szabo
Дата:
On Thu, 11 Jul 2002, [iso-8859-1] frederik nietzsche wrote:

> as if the sigles where not in the "sigles" table, but
> it's there!
> it's probably because of the way in which psql threats
>  the inheritance.
> my question is (finally): is there some workaround for
> this?? or: am I making some mistakes??

Foreign keys don't inherit to children table on either
the fk or pk side.  Note also that the primary keys in
the above will not guarantee that sigle is unique
across the whole set, only across each table individually.

Pretty much the only workaround I know of is to make a table
with the key columns and have each of the tables in the
inheritance tree have its key columns reference that and anything
that wants to reference the inheritance tree references
the other table instead.



Re: how to inherits the references...

От
frederik nietzsche
Дата:
ok, thanks for the (double: two mails ;) ) help, but
in this way when I insert a record in a child table,
the key must be already present in the "sigles" table,
otherwise it breaks the reference and doesn't insert
anything.
In order to use this solution I must create a set of
function that when I want to insert something in a
child tables it automatically insert BEFORE, the sigle
in the "sigles" table and THEN insert the values in
the child table.
If this is the only way, I'm going to use it, but I'm
not really satisfied by it...


ciao 
danilo

> Foreign keys don't inherit to children table on
> either
> the fk or pk side.  Note also that the primary keys
> in
> the above will not guarantee that sigle is unique
> across the whole set, only across each table
> individually.
> 
> Pretty much the only workaround I know of is to make
> a table
> with the key columns and have each of the tables in
> the
> inheritance tree have its key columns reference that
> and anything
> that wants to reference the inheritance tree
> references
> the other table instead.
> 
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster 

______________________________________________________________________
Scarica il nuovo Yahoo! Messenger: con webcam, nuove faccine e tante altre novità.
http://it.yahoo.com/mail_it/foot/?http://it.messenger.yahoo.com/


Re: how to inherits the references...

От
Stephan Szabo
Дата:
On Fri, 12 Jul 2002, [iso-8859-1] frederik nietzsche wrote:

> ok, thanks for the (double: two mails ;) ) help, but
> in this way when I insert a record in a child table,
> the key must be already present in the "sigles" table,
> otherwise it breaks the reference and doesn't insert
> anything.
> In order to use this solution I must create a set of
> function that when I want to insert something in a
> child tables it automatically insert BEFORE, the sigle
> in the "sigles" table and THEN insert the values in
> the child table.

A before trigger to insert into the key table would
probably work.  Another advantage to this sort of thing
is that you could probably actually guarantee uniqueness
of your key across the entire inheritance hierarchy
which you can't otherwise.

> If this is the only way, I'm going to use it, but I'm
> not really satisfied by it...

Inheritance is only marginally useful right now.