Re: Read-only column - Mailing list pgsql-general

From Chris Travers
Subject Re: Read-only column
Date
Msg-id 1071326902.2212.841.camel@localhost.localdomain
Whole thread Raw
In response to Re: Read-only column  (Doug McNaught <doug@mcnaught.org>)
Responses Re: Read-only column
List pgsql-general
Or, depending if you just want to ignore updates to that field (not
always best, but possible, similar to a view).

CREATE OR REPLACE FUNCTION block_col()
RETURNS TRIGGER AS '
    BEGIN
        NEW.ts_field := OLD.ts_field;
        RETURN NEW;
    END;
' LANGUAGE PLPGSQL;

In place of the assignment, you could also test for inequality and raise
an error as Doug suggested:
        IF NEW.ts_field != OLD.ts_field THEN
            RAISE EXCEPTION ''Update to % Not Permitted'',
                ts_field
        END IF;
Best Wishes,
Chris Travers


On Sat, 2003-12-13 at 23:24, Doug McNaught wrote:
> Claudio Succa <claudio.succa.ml@pertel.it> writes:
>
> > (Not to reinvent the wheel, do you know where I could find a suitable
> > function to use in the trigger?)
>
> No, but it should be pretty trivial to write.  Just set up a BEFORE
> UPDATE trigger that compares OLD.ts_field against NEW.ts_field and
> does a RAISE ERROR if they're different.  The PL/pgSQL docs have a few
> decent examples of how to write a trigger function.
>
> -Doug
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>


pgsql-general by date:

Previous
From: Doug McNaught
Date:
Subject: Re: Read-only column
Next
From: Josué Maldonado
Date:
Subject: Why is postgres an ORDMS