Re: Read-only column

From: Chris Travers <chris(at)travelamericas(dot)com>
To: Doug McNaught <doug(at)mcnaught(dot)org>
Cc: Claudio Succa <claudio(dot)succa(dot)ml(at)pertel(dot)it>, pgsql-general(at)postgresql(dot)org
Subject: Re: Read-only column
Date: 2003-12-13 14:48:24
Message-ID: 1071326902.2212.841.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

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(dot)succa(dot)ml(at)pertel(dot)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(at)postgresql(dot)org
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Claudio Succa 2003-12-13 15:19:57 Re: Read-only column
Previous Message Chris Travers 2003-12-13 14:21:02 Re: [NOVICE] PostgreSQL Training

Browse pgsql-hackers by date

  From Date Subject
Next Message Claudio Succa 2003-12-13 15:19:57 Re: Read-only column
Previous Message Claudio Natoli 2003-12-13 07:24:28 fork/exec patch