Foreign keys referencing parent table fails on insert

From: postgresbugs <postgresbugs(at)grifent(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: Foreign keys referencing parent table fails on insert
Date: 2004-12-01 19:58:46
Message-ID: 41AE2276.40107@grifent.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Foreign keys referencing parent table fails on insert if the key is in
the table that inherits the parent table.

I am using psql (PostgreSQL) 7.4.2. I have created a trivial case for an
example.

create table trivial1 (id numeric primary key, numval numeric);
create table trivial2 (textval text)inherits (trivial1);
insert into trivial2 values(1,1,'test1');
create table trivial3 (id numeric, subkey numeric,CONSTRAINT trivial3_c1
PRIMARY KEY (id, subkey),FOREIGN KEY (id) REFERENCES trivial1(id));
insert into trivial3 VALUES (1,0);
ERROR: insert or update on table "trivial3" violates foreign key
constraint "$1"
DETAIL: Key (id)=(1) is not present in table "trivial1".
select * from trivial1;
id | numval
----+--------
1 | 1
(1 row)

While it is true in the strict sense that the key is not in table
"trivial1", the key is in the table "trivial1" in the general sense
since a select on the table does retrieve the key value.

There is an additional problem in that the primary key is not enforced
across the parent (inherited) and child (inheriting) tables. This really
should not work but does.
insert into trivial1 values (1,1);
INSERT 640907 1
A subsequent select give the impression of a violated primary key. I
realize this is not true in the strict sense but appears as a violation
because of the inheritance.
select * from trivial1;
id | numval
----+--------
1 | 1
1 | 1
(2 rows)

Creating trivial2 using
create table trivial2 (textVal text, primary key (id))inherits
(trivial1);
does not change the results.

Am I missing something?

Regards,
John Griffiths

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2004-12-01 20:01:16 Re: Ambiguous behaviour of age(timestamp)
Previous Message Bruce Momjian 2004-12-01 17:39:17 Re: BUG #1319: Windows LIB file libecpg.lib not in build or package