Re: ON DELETE rule - Mailing list pgsql-sql

From Tom Lane
Subject Re: ON DELETE rule
Date
Msg-id 446.986153695@sss.pgh.pa.us
Whole thread Raw
In response to ON DELETE rule  ("J.Fernando Moyano" <txinete@wanadoo.es>)
List pgsql-sql
"J.Fernando Moyano" <txinete@wanadoo.es> writes:
> I'm trying to do something like this:

> CREATE RULE piezas_delete AS
>     ON delete TO piezas
>     DO update materia_prima set usadas=(usadas-1)
>     where n_material=old.n_material;

> The rule is created. OK .... but the action doesn't run like it's supossed.

This will almost certainly not do what you intended, at least not for
cases in which a DELETE deletes multiple rows from piezas.  The rule
creates an additional query that will be run once for each DELETE query
against piezas.  You appear to be envisioning one firing per tuple
deleted.  I'd recommend doing the UPDATE inside a trigger instead.
That approach will operate in the way you envision.
        regards, tom lane


pgsql-sql by date:

Previous
From: Stef Telford
Date:
Subject: Re: Trigger Function and Html Output
Next
From: Sharmad Naik
Date:
Subject: passing parameters between forms