pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ed Loehr <eloehr@austin.rr.com>
To: roberts@panix.com
Cc: pgsql-hackers@postgresql.org
Cc: pgsql-sql@postgresql.org
Subject: Re: [HACKERS] Help with pl/pgsql, triggers, and foreign keys
Date: Sat, 29 Jan 2000 02:37:21 -0600
Message-ID: <3892A6C1.264C62B7@austin.rr.com> (raw)
References: <m27lgt1p7l.fsf_-_@tycho.rlent.pnet>

Roland Roberts wrote:
> 
> The Postgres guide says that foreign keys can be partially emulated
> via triggers.  Just how "partial" is this.  I can't seem to get the
> following to work.  Would it work if I wrote it in C?  Would I need to
> open a second connection to the database?  Would it work if my second
> key was really in another table?
> 
> project=> CREATE FUNCTION check_task_pid () RETURNS OPAQUE AS '
> project'>     BEGIN
> project'>         IF NEW.task_pid IS NOT NULL THEN
> project'>             SELECT task_id FROM task WHERE task_id = NEW.task_pid;
> project'>             IF NOT FOUND THEN
> project'>                 RAISE EXCEPTION ''FOREIGN KEY constraint task_pid not found'';
> project'>             END IF;
> project'>         END IF;
> project'>         RETURN NEW;
> project'>     END;
> project'> ' LANGUAGE 'plpgsql';
> CREATE
> project=> CREATE TRIGGER iu_btrig_check_task_pid BEFORE INSERT OR UPDATE ON task
> project->     FOR EACH ROW EXECUTE PROCEDURE check_task_pid();

At least in 6.5.2, you can definitely implement referential integrity
(RI) via pl/pgsql.  As someone noted earlier, RI is to be released in
7.0, but I suspect it will take a subsequent release or two to
stabilize before it's fit for consumption by the more conservative
reliability-focused users among us...

As for your failing SELECT query, the following tweak to your function
makes it work as expected:

CREATE FUNCTION check_task_pid () RETURNS OPAQUE AS '
    DECLARE
        tmp RECORD;
    BEGIN
        IF NEW.task_pid IS NOT NULL THEN
            SELECT INTO tmp task_id FROM task WHERE task_id =
NEW.task_pid;
            IF NOT FOUND THEN
                RAISE EXCEPTION ''FOREIGN KEY constraint task_pid not
found'';
            END IF;
        END IF;
        RETURN NEW;
    END;
' LANGUAGE 'plpgsql';


Cheers,
Ed Loehr



view thread (4+ messages)  latest in thread

Message-ID: <3892A6C1.264C62B7@austin.rr.com>
Permalink:  ../3892A6C1.264C62B7@austin.rr.com/
Also on:    postgresql.org/message-id/3892A6C1.264C62B7@austin.rr.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: eloehr@austin.rr.com, roberts@panix.com, pgsql-hackers@postgresql.org
  Subject: Re: [HACKERS] Help with pl/pgsql, triggers, and foreign keys
  In-Reply-To: <3892A6C1.264C62B7@austin.rr.com>
  References: <m27lgt1p7l.fsf_-_@tycho.rlent.pnet> <3892A6C1.264C62B7@austin.rr.com>

* 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