Re: Update instead rules on Views

From: Markus Schaber <schabios(at)logi-track(dot)com>
To: PostgreSQL SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Update instead rules on Views
Date: 2004-11-02 15:42:36
Message-ID: 20041102164236.639f10c9@kingfisher.intern.logi-track.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

On Tue, 2 Nov 2004 16:20:37 +0100
Markus Schaber <schabios(at)logi-track(dot)com> wrote:

> > -- But to remain compatibility with old apps, we also need to manage
> > -- updates to the view, which are to be rewritten as follows:
> > CREATE RULE testview_update_rule
> > AS ON UPDATE TO testview DO INSTEAD
> > UPDATE realdata SET
> > index = NEW.index,
> > data = NEW.data,
> > obsolete=NULL
> > ;
>
> I now got it to work with adding a "WHERE index=NEW.index" to the view.

This seems only to work when I update on the INDEX row. I now modified
the rule to look like:

CREATE RULE testview_update_rule
AS ON UPDATE TO testview DO INSTEAD
UPDATE realdata SET
index = NEW.index,
data = NEW.data,
obsolete=NULL
WHERE index = OLD.index;

This seems to work now for arbitrary columns, provided that index is an
unique row.

When I have a table that looks like

index | data | obsolete
-------+-------+----------
2 | test |
3 | blubb |
1 | nono |
3 | hallo |

and I issue

viewtest=# update testview set data='blob' where data='hallo';

I get:
UPDATE 2

and it really updated 2 rows.

As far as I understand now, I need a primary key in the underyling table
as the qualification from the original statemet is applied to the view
results, and not propagated to the underlying table.

Thanks,
Markus

--
markus schaber | dipl. informatiker
logi-track ag | rennweg 14-16 | ch 8001 zürich
phone +41-43-888 62 52 | fax +41-43-888 62 53
mailto:schabios(at)logi-track(dot)com | www.logi-track.com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message John B. Scalia 2004-11-02 15:44:41 query using a date field that isn't set
Previous Message Achilleus Mantzios 2004-11-02 15:21:06 Re: 'show databases' in psql way?