Re: Referencing Problem/bug?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: J A Stride <j(dot)a(dot)stride(at)ncl(dot)ac(dot)uk>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Referencing Problem/bug?
Date: 2003-03-28 23:04:58
Message-ID: 20030328145716.A98607-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Fri, 28 Mar 2003, J A Stride wrote:

> I have a problem with referencing keys. I have several tables:
>
> Resource table with a unique serial:
>
> create table resource (id bigserial not null unique, type int, units int not
> null, stdrate int not null default 0, projectid bigint references project(id)
> on update cascade on delete cascade not null, notes varchar, primary key
> (id));
>
> A person resource that inherits from the resource:
>
> create table personresource (personid bigint references person(id) on update
> cascade on delete cascade not null,primary key (projectid,personid)) inherits
> (resource);
>
> A resource group:
>
> create table resourcegroup (id bigserial not null unique, projectid bigint
> references project(id) on update cascade on delete cascade not null, personid
> bigint references person(id) on update cascade on delete cascade not null,
> name varchar not null, primary key(projectid, name));
>
> A cross reference table that contains all the resources in the resource group:
>
> create table resourcegroupxref (resourcegroupid bigint references
> resourcegroup(id) on update cascade on delete cascade not null, resourceid
> bigint references resource(id) on update cascade on delete cascade not null,
> primary key(resourcegroupid,resourceid));
>
> However if I do an insert into the person resource (which means that there is
> a value in the resource table) and the resource group I cannot put the id's
> from the insert's into the cross reference table as it fails with:
>
> ERROR: <unnamed> referential integrity violation - key referenced from
> resourcegroupxref not found in resource
>
> Is there a reason that I am missing as to why I cannot reference a value in a
> table that is inherited from?

Foreign key constraints currently go only to the explicitly named table
(and not any sub-tables). Inheritance needs a bunch of work in general
(for example, AFAIK you can insert duplicate id values into person in the
above table schema if you specify the column, and even if you put a unique
constraint on it explicitly, you could duplicate values in resource).

There've been some workaround suggestions discussed in the past that
should be in the archives (although none were pretty as i remember)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message jack 2003-03-29 00:21:32 php - postgreSQL link module
Previous Message Dennis Gearon 2003-03-28 22:56:19 Re: Referencing Problem/bug?