【问题标题】:Liquibase ignores autoIncrement="true" for Oracle DBLiquibase 忽略 Oracle DB 的 autoIncrement="true"
【发布时间】:2020-06-10 16:15:40
【问题描述】:

我通过 Hibernate 生成的 DDL 生成了一个模式/表,这是正确的:

 create table cat_component.organisation (
   id number(19,0) generated as identity,
    archived number(1,0),
    is_in_avaloq_group number(1,0) not null,
    mdm_uuid varchar2(255 char),
    name varchar2(255 char),
    primary key (id)
)

之后,我尝试生成如下所示的 Liquibase 更改日志:

 <changeSet author="blabla (generated)" id="1582733383680-5">
    <createTable tableName="organisation">
        <column autoIncrement="true" name="id" type="NUMBER(19, 0)">
            <constraints primaryKey="true" primaryKeyName="organisationPK"/>
        </column>
        <column name="archived" type="NUMBER(1, 0)"/>
        <column name="is_in_avaloq_group" type="NUMBER(1, 0)">
            <constraints nullable="false"/>
        </column>
        <column name="mdm_uuid" type="VARCHAR2(255 CHAR)"/>
        <column name="name" type="VARCHAR2(255 CHAR)"/>
    </createTable>
</changeSet>

问题是,如果我尝试运行 liquibase 更改日志,它会将 XML 定义转换为:

2020-02-26 16:15:10.779  INFO 8064 --- [main] liquibase.executor.jvm.JdbcExecutor      : CREATE TABLE CAT_COMPONENT.organisation (id NUMBER(19, 0) NOT NULL, archived NUMBER(1, 0), is_in_avaloq_group NUMBER(1, 0) NOT NULL, mdm_uuid VARCHAR2(255 CHAR), name VARCHAR2(255 CHAR), CONSTRAINT organisationPK PRIMARY KEY (id))

2020-02-26 16:15:10.787 INFO 8064 --- [main] liquibase.changelog.ChangeSet:表组织已创建 2020-02-26 16:15:10.787 INFO 8064 --- [main] liquibase.changelog.ChangeSet:ChangeSet 类路径:/db/changelog/db.changelog-master.xml::1582733383680-5::blabla(生成) 9ms成功运行

您可能已经注意到,缺少“作为身份生成”,这 让我相信无论出于何种原因, autoIncrement="true" 被忽略。

我使用 Oracle 12.1.0.2 和 org.liquibase:liquibase-core 3.8.2。 OJDBC 驱动程序版本为 19.3.0.0。 gradle 属性是:

spring.datasource.url=jdbc:oracle:thin:@//host:1522/SID
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.username=user
spring.datasource.password=pass
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
spring.jpa.show-sql=true

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type=trace

spring.datasource.continue-on-error=true
spring.datasource.platform=oracle

spring.liquibase.url=${spring.datasource.url}
spring.liquibase.user=${spring.datasource.username}
spring.liquibase.password=${spring.datasource.password}
spring.liquibase.url=${spring.datasource.url}
spring.liquibase.user=${spring.datasource.username}
spring.liquibase.password=${spring.datasource.password}
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
spring.liquibase.default-schema=${spring.jpa.properties.hibernate.default_schema}
spring.liquibase.liquibase-schema=${spring.jpa.properties.hibernate.default_schema}

我看过网上,知道我的Oracle版本兼容这个列类型,不需要创建额外的oracle序列。 任何线索可能是什么问题?

【问题讨论】:

    标签: oracle liquibase liquibase-hibernate liquibase-sql


    【解决方案1】:

    这似乎是 Liquibase 中的一个错误。我刚刚在这里找到它https://liquibase.jira.com/projects/CORE/issues/CORE-3524?filter=allissues&orderby=updated%20DESC&keyword=Oracle

    正如它所说,有一个解决方法来添加

     generationType="BY DEFAULT"
    

    在每个列定义处使用了 autoIncrement="true"。

    【讨论】:

      【解决方案2】:

      更正 autoIncrement="true" 被忽略,因为 Oracle 端缺少对自动递增列的支持,如 liquienter link description here 的文档中所示:

      列是自增列。在没有的数据库上被忽略 支持自动增量/身份功能。

      您将需要create a sequence 来生成 ID 并在插入数据时向id 列提供序列,即使用hibernate

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XXX_ID_GEN")
        @SequenceGenerator(name = "XXX_ID_GEN", sequenceName = "SEQ_XXX")
        long id;
      

      【讨论】:

      • Oracle DB 从 12.1 版开始就具有自动增量功能 - 这就是原始海报使用 create table 语法 generated as identity 显示的内容
      • @ChristopherJones 是对的,我的 Oracle 版本支持它。我们怀疑这是库文档/版本中的错误。我将尝试降级到 liquibase-core 3.5,因为 3.6+ 需要一些未生成的额外属性。
      • 是的 - 感谢您的通知。我还在使用旧版本的 liquibase...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多