Re: Using new.id in the WHERE condition of one UPDATE inside ON INSERT rule

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Aron <auryn(at)wanadoo(dot)es>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Using new.id in the WHERE condition of one UPDATE inside ON INSERT rule
Date: 2010-02-25 15:28:56
Message-ID: 3702.1267111736@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Aron <auryn(at)wanadoo(dot)es> writes:
> I would like to avoid using "SELECT MAX(id) FROM my_table" (it doesn't seem a
> good method), but if I use "new.id", I get new id values, not the "id"
> inserted with the rule, and the condition is always false.

"new.id" is a macro, which in this example will be expanded into a
nextval() function call, which is why it doesn't work --- the nextval()
in the WHERE condition will produce a different value from the one in
the original INSERT. You would be far better off using a trigger here
instead of a rule.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message silly sad 2010-02-26 06:50:11 what exactly is a query structure?
Previous Message Aron 2010-02-25 15:19:32 Using new.id in the WHERE condition of one UPDATE inside ON INSERT rule