首页 文章

将UUID值插入PostgreSQL数据库时的Liquibase问题

提问于
浏览
3

我正在使用带有Liquibase的Spring Boot 2(Core 3.6.2),我的数据库是PostgreSQL . 我在db.changelog-master.xml中通过此更改集创建表:

<changeSet author="system" id="1">
    <createTable tableName="test">
        <column name="id" type="UUID">
            <constraints nullable="false"/>
        </column>
        <column name="note" type="VARCHAR(4096)"/>
    </createTable>
</changeSet>

用于从csv文件向此表插入值的下一个变更集:

<changeSet author="system" id="2">
    <loadData encoding="UTF-8" file="classpath:liquibase/data/test.csv" quotchar="&quot;" separator="," tableName="test">
        <column header="id" name="id" type="STRING" />
       <column header="note" name="note" type="STRING"/>
    </loadData>
</changeSet>

如果我在列 id 而不是STRING中指定类型UUID,liquibase会告诉我:

loadData type of uuid is not supported. Please use BOOLEAN, NUMERIC, DATE, STRING, COMPUTED or SKIP

test.csv 文件的内容:

"id","note"
"18d892e0-e88d-4b18-a5c0-c209983ea3c0","test-note"

当我运行应用程序时,liquibase创建了表,当它尝试插入值时,我收到此消息:

ERROR: column "id" is of type uuid but expression is of type character varying

问题出在类ExecutablePreparedStatementBase中,它位于liquibase-core依赖项中,以及此类中创建此错误的方法行:

private void applyColumnParameter(PreparedStatement stmt, int i, ColumnConfig col) throws SQLException,
        DatabaseException {
    if (col.getValue() != null) {
        LOG.debug(LogType.LOG, "value is string = " + col.getValue());
        stmt.setString(i, col.getValue());
    }

Liquibase使用JDBC和PreparedStatement来执行查询 . 问题是因为表 test 的列类型是 uuid ,liquibase试图插入 string . 如果我们使用JDBC手动将值插入此表,我们应该使用 PreparedStatementsetObject 方法而不是 setString . 但是如果这个问题位于liquibase-core.jar中,我怎么能解决这个问题呢?有人能帮我吗?

1 回答

  • 3

    这非常疼,但我找到了解决方案 . 您需要在JDBC URL属性中指定参数 stringtype=unspecified . 例如,在application.properties中:

    spring.liquibase.url=jdbc:postgresql://127.0.0.1:5432/postgres?stringtype=unspecified
    

    我希望这个答案会对某人有所帮助 .

相关问题