【发布时间】:2019-03-08 21:07:14
【问题描述】:
我正在使用带有 Liquibase (Core 3.6.2) 的 Spring Boot 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=""" 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
问题出在 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 手动向该表插入值,我们应该使用 PreparedStatement 的 setObject 方法而不是 setString。但是,如果这个问题位于 liquibase-core.jar 中,我该如何解决这个问题?有人能帮我吗?
【问题讨论】:
标签: java postgresql spring-boot database-migration liquibase