Sequence does not reset after truncating the table

前端 未结 6 1842
礼貌的吻别
礼貌的吻别 2021-01-31 16:18

I use SELECT lastval() to get wrong serial id after truncated the table.

when I truncate the table, I use SELECT lastval(), I got the wrong ID/

6条回答
  •  长发绾君心
    2021-01-31 16:48

    The best way to reset a sequence to start back with number 1 is to execute the following after you have successfully truncate it:

    ALTER SEQUENCE __seq RESTART WITH 1
    

    So, for example for the users table it would be:

    ALTER SEQUENCE users_id_seq RESTART WITH 1
    

提交回复
热议问题