【问题标题】:Liquibase: Change column to an autoincrement column (identity) in DB2Liquibase:将列更改为 DB2 中的自动增量列(标识)
【发布时间】:2019-05-29 10:23:35
【问题描述】:

我正在尝试将 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

【问题讨论】:

  • 您希望它成为主键吗?
  • 是但没必要...

标签: java spring db2 liquibase


【解决方案1】:

标识列不能为空,这就是error message tells you

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

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

【讨论】:

    【解决方案2】:

    我自己在this link 找到了答案,解释是这样的:

    每个 DB2 管理员迟早会发现 DB2 无法添加 身份列轻松地添加到现有表中。以下语句失败 由于语法错误:alter table public.clicks add column id integer 始终作为身份生成但我对每个人都有好消息。它 仍然可以完成,但需要更多步骤。

    1. 添加非空整数列 您需要为其提供默认值,否则 db2 将拒绝它使其不为空 alter table public.clicks add column id integer not null default 0

    2. 从列中删除默认值 我不确定为什么需要它,因为 Internet 上的一些手册省略了这一步,但我无法在 DB2 9.7.3 LUW 上使用它 alter table public.clicks alter column id drop default

    3. 现在将列设置为始终生成(添加自动增量) alter table public.clicks alter column id set generated always as identity

    4. 重组表使其可写(我没有这样做,它仍然有效) 重组表 public.clicks

    5. 现在用生成的实体值替换零 更新 public.clicks set id = default

    6. 还可以选择为表设置 id 列主键 alter table public.clicks 添加约束 pkey primary key(id)

    以及它的 liquibase 代码:

    defaultSchemaNames 来自我这样定义的 spring 属性。我不知道是否有更好的方法来获取架构,但这个也可以

    # in application.properties I've set this
    liquibase.parameters.defaultSchemaName=MY_SCHEMA_NAME
    

    liquibase.xml

        <property name="defaultSchema" value="${defaultSchemaName}" /> 
    
        <changeSet id="08.01.00" author="alex@mail.com">
            <dropPrimaryKey tableName="members" constraintName="pk_members_relation"/>
        </changeSet>
    
        <changeSet id="08.01" author="alex@mail.com">
            <addColumn tableName="members">
                <column name="id" type="BIGINT" defaultValue="0">
                    <constraints nullable="false"/>
                </column>
    
                <column name="member_type" type="varchar(100)">
                    <constraints nullable="true"/>
                </column>
            </addColumn>
        </changeSet>
    
        <changeSet id="08.02" author="alex@mail.com">
            <dropDefaultValue tableName="members" columnName="id"/>
        </changeSet>
    
        <changeSet id="08.03" author="alex@mail.com">
            <addAutoIncrement tableName="members" columnName="id" columnDataType="bigint"/>
        </changeSet>
    
        <changeSet id="08.04" author="alex@mail.com">
            <sql>
                UPDATE ${defaultSchema}.members SET id = default
            </sql>
        </changeSet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 2018-07-30
      • 2019-07-23
      • 2012-06-06
      • 2016-05-29
      • 1970-01-01
      • 2022-12-10
      相关资源
      最近更新 更多