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

Lists: pgsql-bugs
From: "Martin Edlman" <edlman(at)fortech(dot)cz>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #5495: RI/FK on self and inherited table
Date: 2010-06-09 08:01:48
Message-ID: 201006090801.o5981mVK028904@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 5495
Logged by: Martin Edlman
Email address: edlman(at)fortech(dot)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


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Martin Edlman" <edlman(at)fortech(dot)cz>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #5495: RI/FK on self and inherited table
Date: 2010-06-09 14:32:05
Message-ID: 24422.1276093925@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Martin Edlman" <edlman(at)fortech(dot)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


From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Martin Edlman" <edlman(at)fortech(dot)cz>,<pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #5495: RI/FK on self and inherited table
Date: 2010-06-09 18:56:13
Message-ID: 4C0F9D7D02000025000320EF@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Martin Edlman" <edlman(at)fortech(dot)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