在我的 PostgreSQL 数据库中,我有下表(简化):
CREATE TABLE quotations (
receipt_id bigint NOT NULL PRIMARY KEY
);
CREATE TABLE order_confirmations (
receipt_id bigint NOT NULL PRIMARY KEY
fk_quotation_receipt_id bigint REFERENCES quotations (receipt_id)
);
我的问题现在如下:
我有与之前的报价相关的订单(这很好,因为我可以将这样的订单附加到使用 FK 字段引用的报价中),但我也有从头开始下的订单without匹配的报价。当然,如果数据库允许的话,FK 字段将为 NULL。不幸的是,我得到了一个error当尝试设置时fk_quotation_receipt_id
由于以下原因,在 INSERT 语句中为 NULLviolated外键约束。
在设计这些表时,我仍然使用 PgSQL 8.2,它允许 NULL 值。现在我有 9.1.6,它不允许这样做。
我希望的是一个可选(或可为空)外键约束order_confirmations
(fk_quotation_receipt_id
) → quotations (receipt_id)
。我在 PgSQL 官方文档中找不到任何提示,并且其他用户发布的类似问题已经很老了。
感谢您提供任何有用的提示。
更正了缺少的逗号后,在 9.3 中对我有用。我确信它在 9.1 中也能工作
create table quotations (
receipt_id bigint not null primary key
);
create table order_confirmations (
receipt_id bigint not null primary key,
fk_quotation_receipt_id bigint references quotations (receipt_id)
);
insert into order_confirmations (receipt_id, fk_quotation_receipt_id) values
(1, null);
确认 https://stackoverflow.com/a/3835406/1175496会包括:
INSERT 0 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)