Thread: Table rule does not work!

Table rule does not work!

From
"omid omoomi"
Date:
Hello all,
Here are two master/detail tables .
Table tk ( k1,k2,k3,k4,k5 ) and table tu (u1,u2,u3) where tk.k5 references
tu.u3  .
Now ,I've wrote a rule on table tk as below:
create rule tk_rule1 as on insert to tk do
update tu
set u1 = u1 + (new.k2 - new.k1) ,
    u2 = u2 + (new.k3 + new.k4 )
where tu.u3 = new.k5 ;

when I insert into table tk ,I expect to have table tu updated... but it is
not! although  there is no error message with it, the table tu remains
unchanged.

Any idea about what is wrong?
Is there any mistake with that rule or the 'new' variables?

TIA
Omid Omoomi



_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


Re: Table rule does not work!

From
Tom Lane
Date:
"omid omoomi" <oomoomi@hotmail.com> writes:
> when I insert into table tk ,I expect to have table tu updated... but it is
> not! although  there is no error message with it, the table tu remains
> unchanged.

I cannot duplicate a problem with that rule, using either 7.0.2 or
current sources:

play=> create table tk(k1 int, k2 int, k3 int, k4 int, k5 int);
CREATE
play=> create table tu(u1 int, u2 int, u3 int);
CREATE
play=> insert into tu values (1,1,1);
INSERT 745816 1
play=> insert into tu values (2,2,2);
INSERT 745817 1
play=> create rule tk_rule1 as on insert to tk do
play-> update tu
play-> set u1 = u1 + (new.k2 - new.k1) ,
play-> u2 = u2 + (new.k3 + new.k4 )
play-> where tu.u3 = new.k5 ;
CREATE 745818 1
play=> insert into tk values (7,22,42,17,2);
INSERT 745819 1
play=> select * from tk;
 k1 | k2 | k3 | k4 | k5
----+----+----+----+----
  7 | 22 | 42 | 17 |  2
(1 row)

play=> select * from tu;
 u1 | u2 | u3
----+----+----
  1 |  1 |  1
 17 | 61 |  2
(2 rows)

play=>

Either you made a mistake, or there's some other factor you haven't
mentioned.

            regards, tom lane