pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Paul Lambert <paul.lambert@autoledgers.com.au>
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: Quick question re foreign keys.
Date: Tue, 23 Oct 2007 22:44:40 -0400
Message-ID: <21715.1193193880@sss.pgh.pa.us> (raw)
In-Reply-To: <471EA7D5.9040403@autoledgers.com.au>
References: <471EA7D5.9040403@autoledgers.com.au>

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



view thread (9+ messages)  latest in thread

Message-ID: <21715.1193193880@sss.pgh.pa.us>
Permalink:  ../21715.1193193880@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/21715.1193193880@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, paul.lambert@autoledgers.com.au
  Subject: Re: Quick question re foreign keys.
  In-Reply-To: <21715.1193193880@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox