Foreign key check only if not null? - Mailing list pgsql-general

From Phoenix Kiula
Subject Foreign key check only if not null?
Date
Msg-id CAFWfU=voXQ4p2fNqQ=6RtU0uT+SSgf6T+NDWbCrBKV4yW+9YXw@mail.gmail.com
Whole thread Raw
Responses Re: Foreign key check only if not null?  (Eduardo Piombino <drakorg@gmail.com>)
Re: Foreign key check only if not null?  (Richard Broersma <richard.broersma@gmail.com>)
List pgsql-general
Hi,

I bet this is a simple solution but I have been racking my brains.

I have a column in my table:

    user_id  varchar(100) ;

This can be NULL, or it can have a value. If it has a value during
INSERT or UPDATE, I want to check that the user exists against my
"Users" table. Otherwise, NULL is ok. (Because the functionality in
question is open to both unregistered and registered users).

Any idea on how I can implement a FOREIGN KEY constraint? Or do I need
a pre-insert and pre-update RULE for this?

Thanks!

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Compatibility 9.1rc and 9.1.0
Next
From: John R Pierce
Date:
Subject: 9.0, 9.1 RPM based parallel execution?