on insert when ... do instead update

From: askel <dummy666(at)mail(dot)ru>
To: pgsql-general(at)postgresql(dot)org
Subject: on insert when ... do instead update
Date: 2008-03-16 04:26:48
Message-ID: 72a315ce-2388-418b-955b-6ab8f9ff326f@q78g2000hsh.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello everybody!

I'm having problem with postgresql 8.3 (not sure if it is related to
this particular version). Suppose we have accounting system database
with the following rule (no other rules are there):

create or replace rule update_or_create_balance as
on insert to ledger when exists (select 1 from ledger where
account=new.account) do instead update ledger set debit=debit
+new.debit, credit=credit+new.credit, balance=balance+new.balance
where account=new.account;

What could be the reason of the following considering ledger table is
empty:

insert into ledger values(1,100,0,100);
select * from ledger;
1 | 200.00 | 0.00 | 200.00
(1 row)

Looks like "debit+new.debit" and other expressions are processed as
"debit=new.debit+new.debit" etc. Am I missing something on rules
writing syntax?

Thanks in advance for any help.

Alex

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Volkan YAZICI 2008-03-16 08:01:23 Re: how to make this database / query faster
Previous Message Dave Potts 2008-03-16 01:39:12 Re: Loging of postgres requests