Re: Updating a date field

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Dan C <danc(at)bspmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Updating a date field
Date: 2002-08-19 21:17:30
Message-ID: 20020819141426.I34525-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 19 Aug 2002, Dan C wrote:

> I have a date field called "updated" with a default of now.
> When I INSERT data it the field displays today's date.
> My problem is when I UPDATE the row it doesn't change unless
> I update the field manually like this:
> UPDATE person SET email='doe(at)foo(dot)com',updated='now()' WHERE
> person_id=614486;
> Is there an automatic way?
> I tried doing it with a rule, which returned
> ERROR: query rewritten 10 times, may contain cycles
> I tried a CONSTRAINT and TRIGGER without any luck either.

A before trigger should do it for you (untested)

create function setupdated() returns opaque as '
begin
NEW.updated := now();
return NEW;
end;'
language 'plpgsql';

create trigger person_setupdated before update on
person for each row execute procedure setupdated();

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nigel J. Andrews 2002-08-19 21:34:23 Re: PostgreSQL and MySQL in ZDNet article...
Previous Message Robert J. Sanford, Jr. 2002-08-19 20:58:42 PostgreSQL and MySQL in ZDNet article...