Обсуждение: FW: help with serial

Поиск
Список
Период
Сортировка

FW: help with serial

От
"Luca Rasconi"
Дата:

Hi all,

There’s a behaviour I can’t understand!!!!

There is table TABLE_A with 3 columns (uid, id, session) where uid is the pk and the data type is serial.

There is table TABLE_B with 3 columns (uid, ts_col, col) where uid is the pk.

 

On table TABLE_A I have a rule on insert like this:

 

CREATE OR REPLACE RULE r1 AS

    ON INSERT TO TABLE_A DO  INSERT INTO TABLE_B (uid)

  VALUES ((new.uid));

 

Till now… ok…

When I do an insert

insert into TABLE_A ("id", "session") values (7, 'aaaaaaaaaaaaa');

 

I have this row,

In table TABLE_A

37; 7; "aaaaaaaaaaaaa"

 

while in table TABLE_B

38; ""; ""

 

how is it possible, why in a table 37 and in the other 37 + 1?

 

Thank you,

Luca

 

 

 

Re: FW: help with serial

От
Richard Huxton
Дата:
Luca Rasconi wrote:
>  
> CREATE OR REPLACE RULE r1 AS
>     ON INSERT TO TABLE_A DO  INSERT INTO TABLE_B (uid) 
>   VALUES ((new.uid));

> how is it possible, why in a table 37 and in the other 37 + 1?

This is almost certainly the "nextval() evaluated twice" issue that 
catches everyone out from time to time. Check the mailing-list archives 
for discussion - it's because the rule acts as a macro, duplicating the 
expression.

--  Richard Huxton  Archonet Ltd