Thread: BUG #2771: NULL values in FOREIGN KEY fields

BUG #2771: NULL values in FOREIGN KEY fields

From
"vincent"
Date:
The following bug has been logged online:

Bug reference:      2771
Logged by:          vincent
Email address:      vincent@iim.nctu.edu.tw
PostgreSQL version: 8.x
Operating system:   windows2k
Description:        NULL values in FOREIGN KEY fields
Details:

When insert a null value in foreign key fields, an error occurs.

example table
===================
CREATE TABLE TGroup (
  C_Group_ID CHAR(8) NOT NULL,
  C_Group_Name VARCHAR(50),
  C_Group_Parent CHAR(8) NOT NULL,
  CONSTRAINT TGroup_pkey PRIMARY KEY(C_Group_ID),
  CONSTRAINT TGroup_C_Group_Parent_fkey FOREIGN KEY (C_Group_Parent)
    REFERENCES TGroup(C_Group_ID)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT
    NOT DEFERRABLE
) WITHOUT OIDS;

The definition of foreign key constraint:

A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY
constraint in another table; it can also be defined to reference the columns
of a UNIQUE constraint in another table. A FOREIGN KEY constraint can
contain null values; however, if any column of a composite FOREIGN KEY
constraint contains null values, verification of all values that make up the
FOREIGN KEY constraint is skipped. To make sure that all values of a
composite FOREIGN KEY constraint are verified, specify NOT NULL on all the
participating columns.

ref: http://msdn2.microsoft.com/en-us/library/ms175464.aspx

Re: BUG #2771: NULL values in FOREIGN KEY fields

From
"Jaime Casanova"
Date:
On 11/20/06, vincent <vincent@iim.nctu.edu.tw> wrote:
> When insert a null value in foreign key fields, an error occurs.
>
> CREATE TABLE TGroup (
>   C_Group_ID CHAR(8) NOT NULL,
>   C_Group_Name VARCHAR(50),
>   C_Group_Parent CHAR(8) NOT NULL,
>   CONSTRAINT TGroup_C_Group_Parent_fkey FOREIGN KEY (C_Group_Parent)

That's because your C_Group_Parent column has a Not Null constraint
and you can't insert null values in a Not Null columns

--
regards,
Jaime Casanova

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs and the universe trying
to produce bigger and better idiots.
So far, the universe is winning."
                                       Richard Cook

Re: BUG #2771: NULL values in FOREIGN KEY fields

From
Euler Taveira de Oliveira
Date:
vincent wrote:

> When insert a null value in foreign key fields, an error occurs.
>
This is not a bug. The error occurs because you're trying to insert NULL
in a NOT NULL field (C_Group_Parent). Try to strip the NOT NULL from
C_Group_Parent field so you can insert NULL in it.


--
  Euler Taveira de Oliveira
  http://www.timbira.com/