dbTalk Databases Forums  

View's rule on delete problem

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


Discuss View's rule on delete problem in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Stanislaw Tristan
 
Posts: n/a

Default View's rule on delete problem - 10-27-2004 , 08:32 PM







CREATE RULE "new_rule2" AS ON DELETE TO "public"."klients_view"
DO INSTEAD (

DELETE
FROM klients
WHERE (klients.klient_id = old.klient_id);

DELETE
FROM klient_services
WHERE (klient_services.klient_id = old.klient_id);
);

It's 2 commands, but executing only first. Why?

Stanislaw Tristan
Kyiv, Ukraine
E-mail: stas7775@i.com.ua

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: View's rule on delete problem - 10-31-2004 , 09:29 AM






"Stanislaw Tristan" <stas7775@i.com.ua> writes:
Quote:
It's 2 commands, but executing only first. Why?
You didn't show us the view, but I suppose it's an inner join of the two
tables? As soon as you delete the row from the first table, there's no
longer any matching row in the view, so the second command finds no OLD
row to join against.

Consider making the view a LEFT JOIN and being sure to delete from the
righthand table first.

regards, tom lane

---------------------------(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
  #3  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: View's rule on delete problem - 10-31-2004 , 09:41 AM



On Thu, Oct 28, 2004 at 04:32:21AM +0300, Stanislaw Tristan wrote:
Quote:
CREATE RULE "new_rule2" AS ON DELETE TO "public"."klients_view"
DO INSTEAD (

DELETE
FROM klients
WHERE (klients.klient_id = old.klient_id);

DELETE
FROM klient_services
WHERE (klient_services.klient_id = old.klient_id);
);

It's 2 commands, but executing only first. Why?
Tom Lane described the problem in another thread a few years ago:

http://archives.postgresql.org/pgsql...6/msg00559.php

"...OLD is essentially a macro for the view. As soon as you delete
a row from foo, there's no longer any such row in the view, so the
delete from bar doesn't find anything to delete."

"What you probably want instead is to make bar reference foo as a
foreign key with ON DELETE CASCADE; then the rule for foobar only
needs to delete from foo explicitly, and the additional delete from
bar is done implicitly by the foreign key trigger."

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

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



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.