Re: Quick question re foreign keys.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Paul Lambert <paul(dot)lambert(at)autoledgers(dot)com(dot)au>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Quick question re foreign keys.
Date: 2007-10-24 02:44:40
Message-ID: 21715.1193193880@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Paul Lambert <paul(dot)lambert(at)autoledgers(dot)com(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2007-10-24 02:51:18 Re: request for help with COPY syntax
Previous Message Phillip Smith 2007-10-24 02:14:57 Re: Quick question re foreign keys.