Re: [SQL] Adding a rule to update a last_changed field

From: Brook Milligan <brook(at)biology(dot)nmsu(dot)edu>
To: grant(at)intekom(dot)com
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] Adding a rule to update a last_changed field
Date: 1999-10-22 16:41:18
Message-ID: 199910221641.KAA04832@biology.nmsu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Oops.

This does not work, as it still calls a new UPDATE and re-invokes the rule.

You are right. Your initial mention of using rules made me think of
all the cases I have used this technique for with views. I forgot
that the two tables are the same. What I meant to suggest (which may
not be relevant if you really have only one table) was create a rule
for a view of your table that will update the last_changed field in
the underlying table. I have found that to be very useful in a bunch
of situations where I commonly have one table with lots of different
views and different ways of inserting/deleting/updating as appropriate
to each view. Then the redirection via an INSTEAD rule and setting
last_* fields with rules is valuable.

Sorry about missing the fact that both of your tables were the same.

> CREATE RULE rule1 AS
> ON UPDATE TO thetable <-- for views these are
> DO INSTEAD UPDATE thetable SET <-- not the same table
> last_changed = now(),
> field1 = new.field1,
> field2 = new.field2,
> ...
> fieldn = new.fieldn
> WHERE id = new.id;

Cheers,
Brook

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-10-22 18:49:38 Re: [SQL] Adding a rule to update a last_changed field
Previous Message Grant Kaufmann 1999-10-22 16:09:54 Re: [SQL] Adding a rule to update a last_changed field