Re: foreign key referencing inheritance parent

From: Samed YILDIRIM <samed(at)reddoc(dot)net>
To: J Lumby <johnlumby(at)hotmail(dot)com>, "pgsql-sql(at)lists(dot)postgresql(dot)org" <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: foreign key referencing inheritance parent
Date: 2020-07-31 07:07:05
Message-ID: 445311596177376@mail.yandex.com.tr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<div><div>Hi John,</div><div> </div><div>Please find answers below.</div></div><div> </div><div>20.07.2020, 17:44, "J Lumby" &lt;johnlumby(at)hotmail(dot)com&gt;:</div><blockquote><p>This has come up before elsewhere e.g.<br /><br /><a href="https://stackoverflow.com/questions/26034752/postgresql-inheritance-and-foreign-key-referencing-parent-table">https://stackoverflow.com/questions/26034752/postgresql-inheritance-and-foreign-key-referencing-parent-table</a><br /><br />but I don't see any mention in any pg mailing list.<br /><br /><br />postgresql permits creation of a foreign key referencing a table which<br />is the parent of child tables via inheritance<br /><br />but enforces that every foreign key value must exist as pkey ONLY in the<br />parent  -  it throws an ERROR otherwise, even if the pkey exists in a<br />child table.</p></blockquote><div>Having of primary key on child table with the same columns doesn't mean that it guarantees uniqueness of a record among parent and child tables. Each primary key provides uniqueness of records only in the table on which they are created.</div><blockquote><p><br />This is problematic (to me) for three slightly different reasons :<br /><br />1)   it is surprising.    The expectation is that a reference to<br />accessing row(s) in a parent table will also search all children,   as<br />with SELECT,  unless the ONLY keyword is specified.<br /><br />which then leads to ...</p></blockquote><div><br />The answer above is also valid here :)</div><blockquote><p><br /><br />2)    it is inconsistent with,  and less useful than,    the somewhat<br />similar CHECK clause in a CREATE/ALTER TABLE :<br /><br />_____________________________________________________<br /><br />          CHECK ( expression ) [ NO INHERIT ]<br /><br />A constraint marked with NO INHERIT will not propagate to child tables.<br /><br />_____________________________________________________<br /><br />For CHECK,   the default is propagation to child tables unless<br />explicitly prevented,  which is consistent with SELECT and the ONLY<br />qualifier.<br /><br />For FOREIGN KEY there is no optional qualifier to express "propagation" <br />(i.e. propagation of the search for primary key) or "ONLY" and the<br />behaviour is always "ONLY".<br /><br /><br />3)   I am probably wrong but as far as I can tell this behaviour is not<br />documented anywhere.</p></blockquote><div><a href="https://www.postgresql.org/docs/12/ddl-inherit.html#DDL-INHERIT-CAVEATS">https://www.postgresql.org/docs/12/ddl-inherit.html#DDL-INHERIT-CAVEATS</a></div><div><a href="https://www.postgresql.org/docs/12/ddl-partitioning.html">https://www.postgresql.org/docs/12/ddl-partitioning.html</a>  - Title 5.11.3.3. Caveats</div><blockquote><p><br /><br />Would there be any interest in providing a choice,   e.g. an optional  [<br />INHERIT ] on the REFERENCES clause,  in a future release?</p></blockquote><div> </div><div>The most important part is why you need to use foreign key referencing to parent table of inheritance. Depending on your answer, there are multiple solution. For example, if the reason why you need it is that you use table partitioning implemented by using triggers and table inheritance, such as in the document below[1], you can switch declarative partitioning with PostgreSQL 12. PostgreSQL 12 supports foreign keys referencing to partitioned tables. [2]</div><div> </div><div>[1]: <a href="https://www.postgresql.org/docs/12/ddl-partitioning.html#DDL-PARTITIONING-IMPLEMENTATION-INHERITANCE">https://www.postgresql.org/docs/12/ddl-partitioning.html#DDL-PARTITIONING-IMPLEMENTATION-INHERITANCE</a></div><div>[2]: <a href="https://www.postgresql.org/docs/12/sql-createtable.html">https://www.postgresql.org/docs/12/sql-createtable.html</a></div><ul><li>When establishing a unique constraint for a multi-level partition hierarchy, all the columns in the partition key of the target partitioned table, as well as those of all its descendant partitioned tables, must be included in the constraint definition.</li><li><code>PRIMARY KEY</code> constraints share the restrictions that <code>UNIQUE</code> constraints have when placed on partitioned tables.</li></ul><div>If table partitioning is not the reason, the most generalized solution can be to use constraint triggers[3] rather than foreign keys. And you can guarantee uniqueness of records among parent and child tables by using check constraints and primary key/unique key constraints together.</div><div> </div><div>[3]: <a href="https://www.postgresql.org/docs/12/sql-createtrigger.html">https://www.postgresql.org/docs/12/sql-createtrigger.html</a></div><div> </div><blockquote><p><br />Cheers,  John<br /><br /> </p></blockquote><div>Best regards.</div><div>Samed YILDIRIM</div>

Attachment Content-Type Size
unknown_filename text/html 4.7 KB

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message John Lumby 2020-07-31 15:48:28 Re: foreign key referencing inheritance parent
Previous Message Bhupendra Babu 2020-07-31 06:05:54 Re: how to use function from different database