Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: NULL value in foreign key column

In my PostgreSQL database I have the following tables (simplified):

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) ); 

My problem now reads as follows:

I have orders which relate to previous quotations (which is fine 'cause I can attach such an order to the quotation referenced by using the FK field), but I also have placed-from-scratch orders without a matching quotation. The FK field would then be NULL, if the database let me, of course. Unfortunately, I get an error when trying to set fk_quotation_receipt_id to NULL in an INSERT statement because of a violated foreign key constraint.

When designing these tables I was still using PgSQL 8.2, which allowed NULL values. Now I've got 9.1.6, which does not allow for this.

What I wish is an optional (or nullable) foreign key constraint order_confirmations (fk_quotation_receipt_id) → quotations (receipt_id). I can't find any hints in the official PgSQL docs, and similar issues posted by other users are already quite old.

Thank you for any useful hints.

like image 788
Neppomuk Avatar asked Apr 27 '14 16:04

Neppomuk


People also ask

Can we insert null in foreign key column PostgreSQL?

The FK field would then be NULL, if the database let me, of course. Unfortunately, I get an error when trying to set fk_quotation_receipt_id to NULL in an INSERT statement because of a violated foreign key constraint. When designing these tables I was still using PgSQL 8.2, which allowed NULL values.

Can we have NULL values in foreign key column?

A foreign key containing null values cannot match the values of a parent key, since a parent key by definition can have no null values. However, a null foreign key value is always valid, regardless of the value of any of its non-null parts.

Can a foreign key contain empty values?

Short answer: Yes, it can be NULL or duplicate. I want to explain why a foreign key might need to be null or might need to be unique or not unique. First remember a Foreign key simply requires that the value in that field must exist first in a different table (the parent table).

How do you handle null values in PostgreSQL?

In PostgreSQL, NULL means no value. In other words, the NULL column does not have any value. It does not equal 0, empty string, or spaces. The NULL value cannot be tested using any equality operator like “=” “!=


1 Answers

Works for me in 9.3 after correcting a missing comma. I'm sure it will work also in 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); 

Confirmation will include:

INSERT 0 1 
like image 106
Clodoaldo Neto Avatar answered Sep 21 '22 21:09

Clodoaldo Neto