gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:09 24 4
gpt4 key购买 nike

我将 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 来执行查询。问题是因为 test 表的列类型是 uuid,而 liquibase 试图插入 string。如果我们使用 JDBC 手动向该表插入值,我们应该使用 PreparedStatementsetObject 方法而不是 setString。但是,如果这个问题位于 liquibase-core.jar 中,我该如何解决这个问题?有人能帮我吗?

最佳答案

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

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

我希望这个答案能帮助到别人。

关于java - 将 UUID 值插入 PostgreSQL 数据库时出现 Liquibase 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52630358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com