微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-将UUID值插入PostgreSQL数据库时出现Liquibase问题

我正在将Spring Boot 2与Liquibase(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中指定类型UUID而不是STRING,那么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

问题出在liquibase-core依赖项中的ExecutablePreparedStatementBase类中,并且此类中的方法行会产生此错误

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执行查询.问题是因为表测试的列类型是uuid,而liquibase试图插入字符串.而且,如果我们使用JDBC手动向该表中插入值,则应使用PreparedStatement的setobject方法而不是setString.但是,如果此问题位于liquibase-core.jar中,该如何解决此问题?有人能帮我吗?

解决方法:

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

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

我希望这个答案可以帮助某人.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐