currval has not yet been defined this session, how to get multi-session sequences?

前端 未结 5 1101
再見小時候
再見小時候 2020-12-25 11:38

My objective is to get a primary key field automatically inserted when inserting new row in the table.

How to get a sequence going from session to session in Postg

5条回答
  •  被撕碎了的回忆
    2020-12-25 12:10

    This issue seems to be intermittent , For consistency use CTE to get inserted sequence for current session

    WITH inserted AS ( 
             INSERT INTO notifn_main (notifn_dt,stat_id)
         SELECT now(),22 
         FROM notifn 
         RETURNING id )
         SELECT id 
         from inserted INTO tmp_id;
    

提交回复
热议问题