sql - 继承 PostgreSQL
问题描述
我在 PostgreSQL 下有 2 个表:
作为母表(id、created_at ..)的表属性和继承属性的居住表(表面、房间等)。在母亲表上,我有一个多对多关系表。我在“居住”表中添加了结果。
当我尝试插入带有居住 ID 的“property_table”时,出现 SQL 错误,告诉我 property_id ID 不存在。当我去查看财产或居住表时,它就存在。我想在 property_tag 中插入居住 ID(所以属性),我应该强制插入吗?请帮忙
SQL 模式:
CREATE TABLE "property"
(
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
address_id int NOT NULL,
permission int NOT NULL DEFAULT 6,
user_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (address_id) REFERENCES "address"(id),
FOREIGN KEY (user_id) REFERENCES "user"(id)
);
CREATE TABLE habitation (
total_surface float,
living_surface float,
ground_surface float,
ground_surface_unity character varying(15) DEFAULT 'm2',
room integer,
bedroom integer,
floor integer,
level integer,
year integer
) INHERITS (property);
CREATE TABLE property_tag
(
tag_id int NOT NULL,
property_id int NOT NULL,
PRIMARY KEY (tag_id, property_id),
FOREIGN KEY (tag_id) REFERENCES "tag"(id) ON DELETE CASCADE,
FOREIGN KEY (property_id) REFERENCES "property"(id) ON DELETE CASCADE
);
CREATE TABLE "tag"
(
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
name character varying(127) NOT NULL,
user_id int NOT NULL,
color character varying(7) NOT NULL DEFAULT 'A4A4A8',
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
);
解决方案
当您插入表“居住”时,该行物理上仅存在于表“居住”中,而不存在于“属性”中。
您的表“property_tag”引用了表“property”(没有行,因为行在表“居住”中),因此在插入过程中出现错误。
您必须将表“property_tag”中的参考从“property”更改为“habitation”。
推荐阅读
- c++ - C++ 新手。奇怪的平地机尝试异常捕获???如何安抚Grader?
- java - 在卡片视图中使用预期数据
- javascript - 如何使用 JSON 获取谷歌热图 LatLng?
- android - 如何使用 RecyclerView 和 CardView 在 SearchView 中实现多个过滤器?
- python - Pandas 将包含 nan 值的浮点列转换为 int 以进行合并操作
- python-3.x - scapy:如何手动编码 rdata
- cmake - 如何检测 include(SomeModule) 是否有效?
- php - 在codeigniter中成功为ajax请求请求特定控制器的必要“路由设置”应该是什么?
- python - python regex:从字符串中提取数字,未知数字格式
- linux - 如何为 Linux 创建可在 32 位和 64 位上运行的可执行文件?它与 SYSV 和 GNU / Linux 格式有关吗?