Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL:外键列中的值为空_Postgresql_Foreign Keys - Fatal编程技术网

PostgreSQL:外键列中的值为空

PostgreSQL:外键列中的值为空,postgresql,foreign-keys,Postgresql,Foreign Keys,在我的PostgreSQL数据库中,我有以下表格(简化): 创建表引用( 收据id bigint非空主键 ); 创建表订单确认( 收据id bigint非空主键 fk\报价单\收据\ id bigint引用报价单(收据\ id) ); 我的问题如下: 我有与先前报价相关的订单(这很好,因为我可以使用FK字段将此类订单附加到引用的报价中),但我也从零开始下单,但没有匹配的报价。当然,如果数据库允许,FK字段将为NULL。不幸的是,我在INSERT语句中尝试将fk_quote_receipt_id

在我的PostgreSQL数据库中,我有以下表格(简化):

创建表引用(
收据id bigint非空主键
);
创建表订单确认(
收据id bigint非空主键
fk\报价单\收据\ id bigint引用报价单(收据\ id)
);
我的问题如下:

我有与先前报价相关的订单(这很好,因为我可以使用FK字段将此类订单附加到引用的报价中),但我也从零开始下单,但没有匹配的报价。当然,如果数据库允许,FK字段将为NULL。不幸的是,我在INSERT语句中尝试将
fk_quote_receipt_id
设置为NULL时,由于违反了外键约束,因此出现了错误

在设计这些表时,我仍然使用PGSQL8.2,它允许空值。现在我得到了9.1.6,这是不允许的

我希望的是可选(或可空)外键约束
订单确认
fk\u报价单\u收据\u id
)→ <代码>报价单(收据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);
将包括:

INSERT 0 1

缺少的逗号不是问题所在(我只是在复制时忘记了它,grrrrr)。事实证明,客户机代码添加了一个0而不是NULL,因为这是一个准备好的语句,所以我在日志中没有看到它。尽管如此,我还是非常感谢你的回答。