Re: check foreign constraints before delete - Mailing list pgsql-interfaces

From Michael Fuhr
Subject Re: check foreign constraints before delete
Date
Msg-id 20050922033847.GB90411@winnie.fuhr.org
Whole thread Raw
In response to check foreign constraints before delete  ("Robert Wimmer" <seppwimmer@hotmail.com>)
List pgsql-interfaces
On Wed, Sep 21, 2005 at 11:43:23PM +0000, Robert Wimmer wrote:
> i want to write a function in plpgsql, which checks a foreign key 
> constraint violation on deleting  a special value in my table. for example
> 
> CREATE TABLE to (n NAME PRIMARY KEY);
> CREATE TABLE t1 (n_name NAME REFERENCES to(n), id INTEGER ...);
> 
> CREATE FUNCTION check_before_delete(..some value ...) RETURNS INTEGER AS '
> ..
>  IF foreign_key_violation THEN RETURN -1 END IF;
>  RETURN 0;
> ..
> END;

In PostgreSQL 8.0 you could use an EXCEPTION clause:

http://www.postgresql.org/docs/8.0/interactive/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING

-- 
Michael Fuhr


pgsql-interfaces by date:

Previous
From: "Robert Wimmer"
Date:
Subject: check foreign constraints before delete
Next
From: Ibrahim Noorzaie
Date:
Subject: where to get libpq for postgres 8 - please help!