Inheritance and foreign keys

From: Daniel Harris <dan(dot)harris(at)stealthnet(dot)co(dot)uk>
To: pgsql-novice(at)postgresql(dot)org
Subject: Inheritance and foreign keys
Date: 2004-01-14 18:06:17
Message-ID: 40058519.3040508@stealthnet.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I've been reading on the postgres mailing list about the shortfalls of
inheritance and wanted to make sure that my problem is linked to this
popular problem of inheritance not working as you expect.

I'd also like to know, (as I could not find much on the list) the best
recognised solution from users in the know to the following problem:

create table base (id serial primary key, name text);
create table base_sub () inherits(base);

create table link (base_id integer references base);

Note I couldn't reference base_sub as you would expect to do, for the
following error:
ERROR: there is no primary key for referenced table "base_sub"

insert into base_sub (name) values ('one');
insert into link (base_id) values (1);

Gives this error:
ERROR: insert or update on table "link" violates foreign key constraint
"$1"
DETAIL: Key (base_id)=(1) is not present in table "base".

As I understand it the problem is not being able to create the link
table referencing base_sub as the inherited id field doesn't preserve
it's true primary key status.

If anyone knows a nice solution to the problem, I've only been working
with postgres for a couple of weeks and I'm still learning the ropes.
All I know at the moment is that I'd be a shame to have to hack around
this problem with an ugly fix; inheritance and foreign key support
working together properly would be a *very* nice feature to have.

Thanks in advance!

-- Dan H.

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Stephan Szabo 2004-01-14 19:01:30 Re: Inheritance and foreign keys
Previous Message Ralf Bortfeldt 2004-01-14 16:35:48 parser-error