首页 > 解决方案 > Liquibase:将列更改为 DB2 中的自动增量列(标识)

问题描述

我正在尝试将 BIGINT 列更改为 DB2 中的自动增量列,但我似乎无法找到方法。我试着这样做:

    <changeSet id="08.01" author="...">
        <addColumn tableName="table_name">
            <column name="id" type="bigint">
                <constraints nullable="true"/>
            </column>

            <column name="member_type" type="varchar(100)">
                <constraints nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="08.02" author="...">
        <addAutoIncrement tableName="table_name"
                          columnDataType="bigint"
                          columnName="id"/>
    </changeSet>

当它运行时我得到这个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:db/changelog/08-separation.xml::08.01::author:
     Reason: liquibase.exception.DatabaseException: DB2 SQL Error: SQLCODE=-270, SQLSTATE=42997, SQLERRMC=72, DRIVER=4.13.127 [Failed SQL: ALTER TABLE SCHEMATEST.table_name ALTER COLUMN id SET GENERATED BY DEFAULT AS IDENTITY]
    at org.springframework.bean
...
...
...
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set classpath:db/changelog/08separation.xml::08.01::author:
     Reason: liquibase.exception.DatabaseException: DB2 SQL Error: SQLCODE=-270, SQLSTATE=42997, SQLERRMC=72, DRIVER=4.13.127 [Failed SQL: ALTER TABLE SCHEMATEST.table_name ALTER COLUMN id SET GENERATED BY DEFAULT AS IDENTITY]
...
...
...
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-270, SQLSTATE=42997, SQLERRMC=72, DRIVER=4.13.127

标签: javaspringdb2liquibase

解决方案


标识列不能为空,这是错误消息告诉您的内容:

不能将可为空的列更改为标识列。

将列约束更改为nullable="false"我猜。


推荐阅读