Re: Rule problem with OLD / NEW record set (repost)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: maillist(at)shauny(dot)de
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Rule problem with OLD / NEW record set (repost)
Date: 2005-01-29 00:03:04
Message-ID: 28546.1106956984@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ralph Graulich <maillist(at)shauny(dot)de> writes:
> CREATE VIEW view_table1 AS SELECT * FROM table1;

> -- create a rule for update
> CREATE OR REPLACE RULE ru_view_table1_update
> AS
> ON UPDATE TO view_table1 DO INSTEAD
> (
> -- insert a new record with the old id, old version number incremented
> -- by one, versionflag set to 'Y' and the new content
> INSERT INTO table1 (id, version, vnoflag, content) VALUES (OLD.id,
> OLD.version+1, 'Y', NEW.content);
> -- update the old version and set its versionflag to 'N' as it is no
> -- longer the current record
> UPDATE table1 SET vnoflag = 'N' WHERE id = OLD.id AND version = OLD.version;
> );

> It seems like the UPDATE statement updates both the old and the new
> version.

Yes, because (loosely speaking) OLD refers to the view, and once you've
done the INSERT there is now another matching row in the view. Try
doing the UPDATE first, then the INSERT.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-01-29 00:08:21 Re: rowset-returning function mismatch
Previous Message Stephan Szabo 2005-01-28 23:51:59 Re: rowset-returning function mismatch