From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dean Gibson (DB Administrator) <postgresql@ultimeth.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Creating a RULE for UPDATing a VIEW
Date: Sun, 03 May 2009 13:36:42 -0400
Message-ID: <27650.1241372202@sss.pgh.pa.us> (raw)
In-Reply-To: <49FDCB61.6020206@ultimeth.com>
References: <49FDCB61.6020206@ultimeth.com>
"Dean Gibson (DB Administrator)" <postgresql@ultimeth.com> writes:
> So, I tried:
> CREATE RULE _update AS ON UPDATE TO public.yyy DO INSTEAD
> (DELETE FROM private.zzz WHERE key_field = OLD.key_field;
> INSERT INTO private.zzz VALUES( NEW.*) );
Nope, won't work, standard gotcha for rules newbies. As soon as you
delete in the first command, the row no longer exists in the view,
and "new.*" is just a macro for a view reference.
AFAIK there really isn't any way to do it except
ON UPDATE DO INSTEAD
UPDATE private.zzz SET f1 = new.f1, f2 = new.f2, ...
WHERE key_field = old.key_field;
BTW, you should also consider adding RETURNING clauses to these
rules so that UPDATE RETURNING &etc will work on the views.
Here at least you can use "RETURNING *" ...
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, postgresql@ultimeth.com
Subject: Re: Creating a RULE for UPDATing a VIEW
In-Reply-To: <27650.1241372202@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox