使用postgresql数据库在liquibase中设置初始值

Set initial value in liquibase with postgresql database

我有一个 table,我需要它的 ID 从 1000000 开始。 我为 liquibase 设置了以下代码:

        <column name="id" type="bigint" autoIncrement="true" startWith="1000000">
            <constraints primaryKey="true" nullable="false"/>
        </column>

但是在postgres中不行,生成的sequence generator是从1开始的。 有人对此有任何想法吗?如何启用 table 从 liquibase 中与 postgresql 一起使用的值开始? 提前致谢!

仅 alterSequence 无济于事:

    <alterSequence 
            minValue="1000000"
            sequenceName="t1_id_seq"/>

Unexpected error running Liquibase: ERROR: START value (1) cannot be less than M INVALUE (1000000) [Failed SQL: ALTER SEQUENCE public.t1_id_seq MINVALUE 1000000]

并且根据文档,您不能使用 alterSequence, so you have to createSequence

开始序列
<createSequence 
        sequenceName="seq_id"
        startValue="1000000"/>

然后 "assign" 它作为你的 bigint 列的默认值

  <sql dbms="postgresql" endDelimiter=";\n"  splitStatements="true"
        stripComments="true">ALTER TABLE t1 ALTER COLUMN id SET DEFAULT nextval('seq_id');
  </sql>

或最初使用 bigserial 和 sql 重新启动序列:

<changeSet author="Vao" id="1">
    <createTable tableName="t2">
        <column name="id" type="bigserial">
            <constraints primaryKey="true" primaryKeyName="t2_pkey"/>
        </column>
    </createTable>
</changeSet>

<changeSet author="Vao" id="2">
  <sql dbms="postgresql" endDelimiter=";">ALTER SEQUENCE t2_id_seq restart with 1000;
  </sql>
</changeSet>

不幸的是,startWith 似乎不适用于 postgres 数据库。 您可以使用(使用 yaml 配置):

- changeSet:
  id: add-auto-increment-postgres
  author: me
  dbms: postgresql
  changes:
    - createSequence:
        sequenceName: my_sequence
        startValue: 1000
    - addDefaultValue:
        columnName: id
        defaultValueSequenceNext: my_sequence
        tableName: my_table

谢谢!删除并创建新的 - 帮助

  1. dropSequence
  2. 创建序列