Manually update the counter used for generation of primary key in Hibernate?

前端 未结 2 460
悲&欢浪女
悲&欢浪女 2021-01-24 16:41

In my spring project, the tables in database are created automatically by Hibernate using my entity classes as base, but I insert some default values in the table manually (usin

2条回答
  •  天命终不由人
    2021-01-24 17:36

    Call this SQL query once per table to set the sequence to the next free number:

    SELECT setval('tblname_id_seq', max(id)) FROM tblname;
    

    tblname being the actual name of the table.

    Hibernate may use a different naming convention, or the sequence may have been renamed. If you can't find the sequence behind the serial column, check with (per documentation):

    SELECT pg_get_serial_sequence(tblname, column_name)
    

    More details:
    Modify Django AutoField start value
    How to import a CSV to postgresql that already has ID's assigned?

提交回复
热议问题