V
V
Vladislav Shchekoldin2012-04-05 14:43:19
PostgreSQL
Vladislav Shchekoldin, 2012-04-05 14:43:19

Postgres, foreign key to parent(in inheritance mean) table:impossible?

The situation is as follows: there is a common table for all types of documents:

create table document(<br/>
id bigserial,<br/>
title text,<br/>
prinary key(id)<br/>
);

From it there is an inherited table with additional. fields of the form
create table purchase_request(<br/>
...<br/>
primary key()id<br/>
)<br/>
inherits(document)<br/>

Each document is associated with a table of goods of the form:
create table document_items(<br/>
document_id bigint references document(id),<br/>
item_id bigint references items(id),<br/>
)

So, if I first create a record in the document table, then in document_items, everything is fine.
But when I created a record in the purchase_request table, and then I want to add a record to document_items, it screams that such a key does not exist.
What could be the reason?
The answer in this situation turned out to be simple: it is impossible to solve this problem now. Inheritance Caveats

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anatoly, 2012-04-05
@taliban

I'll say shcha for mysql but I think the reason is the same: When you insert a record, and there is a forenkey, then in fact one table is obtained leading, the other slave. That is, to insert a record into a slave table, there must already be a record with such a key in the master table (roughly speaking). Or, when creating a foreign key, remove the actions and allow zero as a value for the field. It's messy, of course, but somehow it works =)

V
Vladislav Shchekoldin, 2012-04-05
@ZiNTeR

It turned out that the answer to this question is in the instructions for Postgres. Literally: There is no good workaround for this case.
www.postgresql.org/docs/9.1/static/ddl-inherit.html

V
vajadhava, 2012-04-05
@vajadhava

Triggers are everything :) Check the integrity manually.

Z
ztxn, 2012-04-06
@ztxn

Thank you for this question. Didn't know about this PG bun.
It is sad that caveats narrow its scope to a negligibly narrow one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question