pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Foreign key referencing subclasses.
4+ messages / 3 participants
[nested] [flat]

* Foreign key referencing subclasses.
@ 2001-03-22 14:18  Johannes Grødem <johs@copyleft.no>
  0 siblings, 2 replies; 4+ messages in thread

From: Johannes Grødem @ 2001-03-22 14:18 UTC (permalink / raw)
  To: pgsql-sql

Hi,

it seems I can't have a foreign key that references some subclass.  Postgres
says it can't figure out what its primary key is.  The primary key is defined
in the superclass.

I have something like this:

CREATE TABLE resource_record(
  rrid SERIAL
  -- etc.
);

CREATE TABLE soa_record(
  -- blah, blah
) INHERITS(resource_record);

CREATE TABLE domain(
  -- ...
  soaid REFERENCES soa_record  -- *
);


* = This doesn't work. I can reference resource_record here, but I can not
    reference soa_record that way. Referencing resource_record* doesn't work
    either.

-- 
johs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Foreign key referencing subclasses.
@ 2001-03-22 17:16  Christof Glaser <gcg@gl.aser.de>
  parent: Johannes Grødem <johs@copyleft.no>
  1 sibling, 1 reply; 4+ messages in thread

From: Christof Glaser @ 2001-03-22 17:16 UTC (permalink / raw)
  To: Johannes Grødem <johs@copyleft.no>; pgsql-sql

Hi Johannes,

On Thursday, 22. March 2001 15:18, Johannes Grødem wrote:
> Hi,
>
> it seems I can't have a foreign key that references some subclass. 
> Postgres says it can't figure out what its primary key is.  The
> primary key is defined in the superclass.
>
> I have something like this:
>
> CREATE TABLE resource_record(
>   rrid SERIAL
>   -- etc.
> );

There is no primary key for this table. Just write PRIMARY KEY after 
SERIAL.

> CREATE TABLE soa_record(
>   -- blah, blah
> ) INHERITS(resource_record);
>
> CREATE TABLE domain(
>   -- ...
>   soaid REFERENCES soa_record  -- *
> );
>
>
> * = This doesn't work. I can reference resource_record here, but I
> can not reference soa_record that way. Referencing resource_record*
> doesn't work either.

Primary keys as well as other indexes are not inherited, unfortunately. 
You should create the indexes for any subclass manually, like:

CREATE INDEX soa_record_pkey ON soa_record ( rrid );

You could also state the referenced field name (you actually have to, 
if you happen to reference to a non-primary key field):

CREATE TABLE ...
 soaid  int4  REFERENCES soa_record ( rrid )
        ^^^^
Note that SERIAL is actually int4 with automagically created sequences.
You can't leave out the column's type for references.

And note also that referencing to a superclass* (including subclasses) 
does not work for 7.0.x. I think it changes with 7.1 but I'm not sure.
You could create your own triggers, though.

Hope that helps,

Christof.
-- 
          gl.aser . software engineering . internet service
       http://gl.aser.de/  . Planckstraße 7 . D-39104 Magdeburg
Tel. +49.391.7 44 77 10 . Fax +49.391.7 44 77 13 . Mobil 0177.77 92 84 3



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Foreign key referencing subclasses.
@ 2001-03-22 17:31  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: Johannes Grødem <johs@copyleft.no>
  1 sibling, 0 replies; 4+ messages in thread

From: Stephan Szabo @ 2001-03-22 17:31 UTC (permalink / raw)
  To: Johannes Grødem <johs@copyleft.no>; +Cc: pgsql-sql


On Thu, 22 Mar 2001, [iso-8859-1] Johannes Grødem wrote:

> Hi,
> 
> it seems I can't have a foreign key that references some subclass.  Postgres
> says it can't figure out what its primary key is.  The primary key is defined
> in the superclass.

Unique/primary key doesn't inherit to subclasses.

If you want to reference just soa_record, you can create the unique index
on soa_record.  If you want to do the entire tree, the fk stuff doesn't
support that (issues with inheriting the triggers mostly).





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Foreign key referencing subclasses.
@ 2001-03-23 13:50  Johannes Grødem <johs@copyleft.no>
  parent: Christof Glaser <gcg@gl.aser.de>
  0 siblings, 0 replies; 4+ messages in thread

From: Johannes Grødem @ 2001-03-23 13:50 UTC (permalink / raw)
  To: pgsql-sql

>> CREATE TABLE resource_record(
>>   rrid SERIAL
>>   -- etc.
>> );
> There is no primary key for this table. Just write PRIMARY KEY after 
> SERIAL.

There is.  I accidentally left it out in the post.

> CREATE INDEX soa_record_pkey ON soa_record ( rrid );
> You could also state the referenced field name (you actually have to, 
> if you happen to reference to a non-primary key field):

Ok, thanks.  This will work for this case, but I still want to be able
to reference resource_record*.  Will this be possible in later versions?
Inheritance isn't quite as useful if I can't.  Sorry to be such a whiner,
by the way.  PostgreSQL is great.

-- 
johs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2001-03-23 13:50 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-03-22 14:18 Foreign key referencing subclasses. Johannes Grødem <johs@copyleft.no>
2001-03-22 17:16 ` Christof Glaser <gcg@gl.aser.de>
2001-03-23 13:50   ` Johannes Grødem <johs@copyleft.no>
2001-03-22 17:31 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox