我可以在PostgreSQL的父表中有一个外键吗? [英] Can I have a foreign key to a parent table in PostgreSQL?

查看:144
本文介绍了我可以在PostgreSQL的父表中有一个外键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用继承,但最终遇到了问题。

I'm using inheritance and I ended up having a problem.

如果我运行:

select count(*) from estate_properties where id = 86820;

我得到1。

但是当我尝试运行以下命令:

But when I try to run this:

insert into property_images (binary_image, name, property_id) values (16779, 'IMG_0096.jpg', 86820)

我得到:


**********错误**********

********** Error **********

错误:在表上插入或更新 property_images违反了外部
键约束 property_images_property_id_fkey SQL状态:23503
详细信息:表(
estate_properties中不存在键(property_id)=(86820)。

ERROR: insert or update on table "property_images" violates foreign key constraint "property_images_property_id_fkey" SQL state: 23503 Detail: Key (property_id)=(86820) is not present in table "estate_properties".

estate_properties 上的 ID SERIAL

注意:另一个表 apartments 继承自 estate_properties ,并添加了86820。那会有所作为吗?还有为什么为什么我在父表中仍然有ID,并且可以从那里选择呢?

Note: Another table apartments inherits from estate_properties, and 86820 was added to it. Would that make a difference? Also why would it I still have the ID in the parent table and I can select if from there.

编辑:
寻找

http://www.postgresql.org/docs/9.5/static/ddl-inherit.html

我想实现以下目标:

5.9.1。注意事项

5.9.1. Caveats



  • 指定另一个表的列引用城市(名称)将

    允许

    在这种情况下,没有很好的解决方法。

EDIT2:
这是外键的声明:

Here is the declaration of the foreign key:

CONSTRAINT property_images_property_id_fkey FOREIGN KEY (property_id)
      REFERENCES estate_properties (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION


推荐答案

显然答案在这里:

PostgreSQL中的外键+表继承?

外键可以指向作为继承层次结构一部分的表,但只能在该表中找到行确切地表。不在任何父表或子表中。若要查看外键看到的行,请执行SELECT * FROM ONLY表。 ONLY关键字的意思是忽略继承,这就是外键查找将要执行的操作

A foreign key can point to a table that is part of an inheritance hierarchy, but it'll only find rows in that table exactly. Not in any parent or child tables. To see which rows the foreign key sees, do a SELECT * FROM ONLY thetable. The ONLY keyword means "ignoring inheritance" and that's what the foreign key lookup will do

这篇关于我可以在PostgreSQL的父表中有一个外键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆