问题
I wonder if it possible to get maximum column value from a certain table and set it as start sequence value with no pure sql. The following code doesn't work:
<property name="maxId" value="(select max(id)+1 from some_table)" dbms="h2,mysql,postgres"/>
<changeSet author="author (generated)" id="1447943899053-1">
<createSequence sequenceName="id_seq" startValue="${maxId}" incrementBy="1"/>
</changeSet>
Got an error:
Caused by: liquibase.parser.core.ParsedNodeException: java.lang.NumberFormatException: For input string: "${m"
I've tried it with no parentheses around select ...
etc. with the same result.
So it's not possible to use computed value as start sequence value?
回答1:
So, such a solution worked for me:
<changeSet author="dfche" id="1448634241199-1">
<createSequence sequenceName="user_id_seq" startValue="1" incrementBy="1"/>
</changeSet>
<changeSet author="dfche" id="1448634241199-2">
<sql dbms="postgresql">select setval('user_id_seq', max(id)+1) from jhi_user</sql>
<sql dbms="h2">alter sequence user_id_seq restart with (select max(id)+1 from jhi_user)</sql>
</changeSet>
回答2:
For postgres correct sql dbms sentence is:
<sql dbms="postgresql">select setval('some_table_id_seq', (select max(id)+1 from some_table), false)</sql>
来源:https://stackoverflow.com/questions/33888587/set-max-column-value-as-sequence-start-value-with-liquibase-tags