plpgsql - Inserting DEFAULT Value.

From: Vams <vmudrageda(at)charter(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: plpgsql - Inserting DEFAULT Value.
Date: 2004-06-16 01:29:29
Message-ID: 200406152129.29351.vmudrageda@charter.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

I need to protect a SERIAL column from having a value inserted into it (other
than the default sequence) and keep that value from being tampered with. So
I created a function which is called by a trigger. Unfortunately, I don't
know how to assign DEFAULT to the id column. Can anyone tell me how I can
fix my function or is there another easier way that doesn't need triggers or
functions? Here is what I got so far.

CREATE OR REPLACE FUNCTION id_protect()
RETURNS TRIGGER AS '
BEGIN
IF TG_OP = ''INSERT'' THEN
NEW.id := DEFAULT; -- here is the problem :(
RETURN NEW;
ELSIF TG_OP = ''UPDATE'' THEN
NEW.id := OLD.id;
RETURN NEW;
ELSE
RETURN NEW;
END IF;
END;
' LANGUAGE PLPGSQL;

Important point: I need this to be a generic function. I got multiple tables
that have a id column, each with their own unique sequences, and I want to
use the same function for all of them. So NEW.id := nextval('some_sequence')
can't work.

Thank you all,

Vams

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-06-16 01:54:16 Re: PostgreSQL 7.4.3 Now Available ...
Previous Message Doug McNaught 2004-06-15 23:17:00 Re: perl access