Help with distinctly non-intuitive rule behaviour - Mailing list pgsql-sql

From Simon Kinsella
Subject Help with distinctly non-intuitive rule behaviour
Date
Msg-id 20060305211817.2DBF3199CFD@smtp04l.fasthosts.co.uk
Whole thread Raw
Responses Re: Help with distinctly non-intuitive rule behaviour
List pgsql-sql
Hello,

Can this possibly be right?

From my troubleshooting of a DELETE rule it appears that the rule's WHERE
condition sees the *results* of the rule-actions. Apart from being pretty
odd, this is a proving to be a big problem in my situation.

Here is a cut-down example:

Given a table 'users', let's say we would like to 'soft-delete' users from
the table by rewriting DELETE as UPDATE using the rule below. For active
users the users.user_departed field is 'infinity'::TIMESTAMP.  The UPDATE
action sets the timestamp of the user's deletion, if and only if the current
timestamp is in the future:

CREATE RULE rule_soft_delete_user AS ON DELETE TO users WHERE user_departed > now() DO INSTEAD    UPDATE users SET
user_departed= now()     WHERE user_id = OLD.user_id;
 

However, this does not work. I know this because I get a foreign-key
violation resulting from an unmolested DELETE action even for a user who has
not been soft-deleted. If as a test I change the UPDATE action so that it
does something other than change the timestamp then the rule is applied.

This seems pretty weird - can it be true?

---
Simon Kinsella
This message has been scanned for viruses.




pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: functions in WHERE clause
Next
From: Tom Lane
Date:
Subject: Re: Help with distinctly non-intuitive rule behaviour