简体   繁体   English

使用 Liquibase 的 JOOQ 代码生成,保留关键字? (“值”列名称错误)

[英]JOOQ code generation with Liquibase, reserved keyword? (error with "value" column name)

I have a table defined in a liquibase script with a column named "value":我有一个在 liquibase 脚本中定义的表,其中包含一个名为“值”的列:

- column:
    name: value
    type: VARCHAR
    constraints:
      - nullable: false

dbms is postgresql数据库管理系统是 postgresql

When running the JOOQ codegen with the maven plugin, it runs the liquibase script and I get the following error:使用 maven 插件运行 JOOQ codegen 时,它运行 liquibase 脚本,我收到以下错误:

Syntax error in SQL statement "CREATE TABLE PUBLIC.TABLE_NAME (ID BIGINT AUTO_INCREMENT NOT NULL, ... , VALUE[*] VARCHAR NOT NULL)"; expected "identifier";

If I change the column name from "value" to anything else, it works.如果我将列名从“值”更改为其他任何名称,它就会起作用。 With JOOQ up to version 3.15, it works.使用 JOOQ 到 3.15 版,它可以工作。

Any clue on how to handle this?关于如何处理这个的任何线索? I'd rather not change the name, I have multiple tables with a column named "value" so it's a quite big refactor, and naming wise I feel it's the most appropriate name for what it represents.我不想更改名称,我有多个表,其中有一列名为“值”,因此这是一个相当大的重构,并且在命名方面我觉得它是最适合它所代表的名称。

Solution解决方案

This is already fixed in the newer versions of liquibase, so you can manually specify which LB version to use in the jOOQ codegen:这已经在较新版本的 liquibase 中修复,因此您可以手动指定在 jOOQ codegen 中使用哪个 LB 版本:

<plugin>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen-maven</artifactId>
        <version>${jooq.version}</version>
        <dependencies>
          <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>${liquibase.version}</version>
          </dependency>
        ...
</plugin>

Why this happens为什么会这样

The LiquibaseDatabase in jOOQ 3.16's code generation configuration works by simulating your migration against an in-memory H2 database , which, starting from H2 2.0 has incompatibly declared VALUE a keyword, which can no longer be used as an identifier without quoting. jOOQ 3.16 的代码生成配置中的LiquibaseDatabase通过模拟针对内存中 H2 数据库的迁移来工作,从 H2 2.0 开始,该数据库不兼容地声明了VALUE关键字,如果不加引号就不能再用作标识符。

Workaround in Liquibase Liquibase 中的解决方法

So, your workaround could be to quote all objects (or reserved words, if Liquibase is up to date with H2's latest changes): https://docs.liquibase.com/parameters/object-quoting-strategy.html因此,您的解决方法可能是引用所有对象(或保留字,如果 Liquibase 是最新的 H2 的最新更改): https://docs.liquibase.com/parameters/object-quoting-strategy.html

Eg例如

databaseChangeLog:
    -  object-quoting-strategy: QUOTE_ALL_OBJECTS

However, this means that you should make sure to use only lower case identifiers in your Liquibase configuration, as to not accidentally create case sensitive identifiers in your PostgreSQL database但是,这意味着您应确保在 Liquibase 配置中仅使用小写标识符,以免在 PostgreSQL 数据库中意外创建区分大小写的标识符

Upgrading Liquibase升级 Liquibase

I can't reproduce this with the latest versions of Liquibase.我无法使用最新版本的 Liquibase 重现这一点。 It seems they have fixed this and now support H2 2.x correctly看来他们已经解决了这个问题,现在可以正确支持 H2 2.x

A future fix in jOOQ jOOQ 的未来修复

jOOQ should fix this on the jOOQ side. jOOQ 应该在 jOOQ 方面解决这个问题。 Eventually, H2 will be removed from the equation (at least it will be possible to opt out of using it), and jOOQ will interpret the DDL generated by Liquibase directly in order to generate your code.最终,H2 将从等式中移除(至少可以选择不使用它),jOOQ 将直接解释 Liquibase 生成的 DDL 以生成您的代码。 The relevant feature request is: https://github.com/jOOQ/jOOQ/issues/7034相关功能请求为: https://github.com/jOOQ/jOOQ/issues/7034

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM