dbTalk Databases Forums  

Rules WHERE condition

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Rules WHERE condition in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Robert Fitzpatrick
 
Posts: n/a

Default Rules WHERE condition - 11-17-2004 , 10:20 AM






I have a view that used union all to merge three tables together. I was
hoping to create a rule, one for each table using the WHERE condition of
the rule to determine which table gets updated. Is this possible?

This is what I have, assume the view here is a merge of three tables
using union all:

CREATE RULE "update_xrf" AS ON UPDATE TO "public"."viewdeterioratedlbp"
WHERE ((new.note)::text = 'Unit'::text)
DO INSTEAD (UPDATE tblxrf SET deterioration = new.deterioration;

The note column contains a value that can trigger which table needs to
be updated. I would like to make one of these rules for each table to
update. But when I run the update, it says I have to have an
unconditional rule, is that right? Any suggestions?

ohc=# update viewdeterioratedlbp set note = 'Unit', deterioration =
'test' where xrf_id = 733;
ERROR: cannot update a view
HINT: You need an unconditional ON UPDATE DO INSTEAD rule.

Thanks,
--
Robert


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
  #2  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: Rules WHERE condition - 11-17-2004 , 10:49 AM






On Wed, Nov 17, 2004 at 11:20:41AM -0500, Robert Fitzpatrick wrote:

Quote:
I have a view that used union all to merge three tables together. I was
hoping to create a rule, one for each table using the WHERE condition of
the rule to determine which table gets updated. Is this possible?
See the CREATE RULE documentation:

http://www.postgresql.org/docs/7.4/s...reaterule.html

In particular, read the last paragraph of the Description section,
the one that begins, "There is a catch if you try to use conditional
rules for view updates...."

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #3  
Old   
Robert Fitzpatrick
 
Posts: n/a

Default Re: Rules WHERE condition - 11-17-2004 , 11:49 AM



On Wed, 2004-11-17 at 11:49, Michael Fuhr wrote:
Quote:
On Wed, Nov 17, 2004 at 11:20:41AM -0500, Robert Fitzpatrick wrote:

I have a view that used union all to merge three tables together. I was
hoping to create a rule, one for each table using the WHERE condition of
the rule to determine which table gets updated. Is this possible?

See the CREATE RULE documentation:

http://www.postgresql.org/docs/7.4/s...reaterule.html

Thanks, that explains a lot, but still not able to get my rule to work,
this is what I have now:

CREATE RULE "update_unconditional" AS ON UPDATE TO
"public"."viewdeterioratedlbp"
DO INSTEAD NOTHING;

CREATE RULE "update_xrf" AS ON UPDATE TO "public"."viewdeterioratedlbp"
WHERE ((new.note)::text = 'Unit'::text)
DO (UPDATE tblxrf SET deterioration = new.deterioration WHERE
(tblxrf.xrf_id = new.xrf_id);

ohc=# update viewdeterioratedlbp set deterioration = 'test' where xrf_id
= 143;
UPDATE 0

This is the first rule I have tried to setup, I read through the doc,
but don't seem to be able to catch what I'm doing wrong. Do I have to
update all fields for it to work?

--
Robert


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
  #4  
Old   
Robert Fitzpatrick
 
Posts: n/a

Default Re: Rules WHERE condition - 11-17-2004 , 12:28 PM



On Wed, 2004-11-17 at 12:49, Robert Fitzpatrick wrote:
Quote:
On Wed, 2004-11-17 at 11:49, Michael Fuhr wrote:
On Wed, Nov 17, 2004 at 11:20:41AM -0500, Robert Fitzpatrick wrote:

I have a view that used union all to merge three tables together. I was
hoping to create a rule, one for each table using the WHERE condition of
the rule to determine which table gets updated. Is this possible?

See the CREATE RULE documentation:

http://www.postgresql.org/docs/7.4/s...reaterule.html


Thanks, that explains a lot, but still not able to get my rule to work,
this is what I have now:

Forget that last post, it is working even though the UPDATE 0 is
returned. The record did update

Please someone let me know if you see any issues with that? Like I said,
my first rule let alone with a WHERE condition.

--
Robert


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.