Re: Quick question re foreign keys. - Mailing list pgsql-sql

From Tom Lane
Subject Re: Quick question re foreign keys.
Date
Msg-id 21715.1193193880@sss.pgh.pa.us
Whole thread Raw
In response to Quick question re foreign keys.  (Paul Lambert <paul.lambert@autoledgers.com.au>)
Responses Re: Quick question re foreign keys.
List pgsql-sql
Paul Lambert <paul.lambert@autoledgers.com.au> writes:
> I have a table where one column references by foreign key a column from 
> another table. However, the column in this first table does not always 
> contain data which results in a not-null constraint violation when I 
> attempt an insert.

> My question therefore is, is it possible to create a foreign key that is 
> conditional, i.e. only enforce the foreign key where the value in that 
> table is not null.

If I'm understanding you correctly, the problem is not the foreign key,
it's that you marked the column NOT NULL.  A foreign key constraint by
itself will allow a NULL in the referencing column to pass.  You choose
whether you want to allow that or not by separately applying a NOT NULL
constraint or not.
        regards, tom lane


pgsql-sql by date:

Previous
From: "Phillip Smith"
Date:
Subject: Re: Quick question re foreign keys.
Next
From: Adrian Klaver
Date:
Subject: Re: request for help with COPY syntax