BUG #5495: RI/FK on self and inherited table

Started by Martin Edlmanalmost 16 years ago3 messagesbugs
Jump to latest
#1Martin Edlman
edlman@fortech.cz

The following bug has been logged online:

Bug reference: 5495
Logged by: Martin Edlman
Email address: edlman@fortech.cz
PostgreSQL version: 8.4.4
Operating system: Scientific Linux 5.5 (RHEL)
Description: RI/FK on self and inherited table
Details:

I have a table

net.device(id serial, parent_id integer null, name varchar(100), ip_address
inet, ...)
device_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES net.device(id) ON
UPDATE CASCADE

and a table

net.computer(parent_id integer not null, ...) inherits (net.device);

I have a script which inserts records to both tables and it always stops on
the same record for net.computer with followin message

ERROR: insert or update on table "device" violates foreign key constraint
"device_parent_id_fkey"
DETAIL: Key (parent_id)=(19947) is not present in table "device".

But the record is there, it was inserted into net.computer so it's
selectable from net.device and from net.computer.

select id,parent_id,name,ip_address from net.device where id = 19947;
id | parent_id | name | ip_address
-------+-----------+-----------------+--------------------
19947 | 1649 | pc-lit-customer | 213.213.213.213/30

select id,parent_id,name,ip_address from net.computer where id = 19947;
id | parent_id | name | ip_address
-------+-----------+-----------------+--------------------
19947 | 1649 | pc-lit-digistyl | 213.250.201.222/30

There are other records in the net.computer which are referenced in other
records as the parent_id.

It really puzzles me.

If you want I can send you full definition of the net.device and the
net.computer including indices, triggers etc.

I have inserted 1795 records to net.device

select count(*) from only net.device;
count
-------
1795

and 5105 records to net.computer

select count(*) from only net.computer;
count
-------
5105

That's 6900 records in total

select count(*) from net.device;
count
-------
6900

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Martin Edlman (#1)
Re: BUG #5495: RI/FK on self and inherited table

"Martin Edlman" <edlman@fortech.cz> writes:

[ FK doesn't see a key that is present in a child table ]

That's how FK checks work, see the "Caveats" section at the bottom of
http://www.postgresql.org/docs/8.4/static/ddl-inherit.html

regards, tom lane

#3Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Martin Edlman (#1)
Re: BUG #5495: RI/FK on self and inherited table

"Martin Edlman" <edlman@fortech.cz> wrote:

ERROR: insert or update on table "device" violates foreign key
constraint "device_parent_id_fkey"
DETAIL: Key (parent_id)=(19947) is not present in table "device".

But the record is there, it was inserted into net.computer so it's
selectable from net.device and from net.computer.

This is not a bug, but a known limitation. Bring up this page and
look for the places it mentions "foreign key":

http://www.postgresql.org/docs/8.4/interactive/ddl-inherit.html

-Kevin