POSTGRESQL Foreign Key Referencing Primary Keys of two Different Tables

后端 未结 4 622
旧时难觅i
旧时难觅i 2020-12-02 10:24

I have two tables Books and Audiobooks, both of which have ISBN as their primary keys. I have a table writtenby that has an isbn attribute that has a foreign key constraint

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 11:10

    RDBMS's do not support polymorphic foreign key constraints. What you want to do is reasonable, but not something accommodated well by the relational model and one of the real problems of object relational impedance mismatch when making ORM systems. Nice discussion on this on Ward's WIki

    One approach to your problem might be to make a separate table, known_isbns, and set up constraints and/or triggers on Books and AudioBooks so that table contains all the valid isbns of both type specific book tables. Then your FK constraint on writtenby will check against known_isbns.

提交回复
热议问题