Foreign keys + table inheritance in PostgreSQL?

后端 未结 3 962
囚心锁ツ
囚心锁ツ 2020-12-30 22:05

I have three tables: organization, organization_teams and org_users. Here organization_teams is inherited from orga

3条回答
  •  暖寄归人
    2020-12-30 22:14

    Completely agree with @Craig Ringer, we can't use foreign keys along with inheritance.

    But if we are sure that we are going to insert correct data, and we want to use foreign key in org_users, we can do one thing.

    We can create a child table of org_users say org_users_child (with no foreign key) as below

    CREATE TABLE org_users_child () INHERITS (org_users);
    

    Now we can insert data in this child table.

    Now if we query over org_users table, we can find desired results.

提交回复
热议问题