将 UUID 值插入 PostgreSQL 数据库时出现 Liquibase 问题
Liquibase problem when inserting UUID value to PostgreSQL Database
我正在使用 Spring Boot 2 和 Liquibase (Core 3.6.2),我的数据库是 PostgreSQL。
我正在 db.changelog-master.xml:
中通过这个变更集创建 table
<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 文件向此 table 插入值的下一个变更集:
<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 创建了 table 并且当它尝试插入值时我收到此消息:
ERROR: column "id" is of type uuid but expression is of type character varying
问题出在 class ExecutablePreparedStatementBase 中,它位于 liquibase-core 依赖项中,并且此 class 中的方法行造成此错误:
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 来执行查询。问题是因为 table test 的列类型是 uuid,而 liquibase 试图插入 string。如果我们使用 JDBC 手动向 table 插入值,我们应该使用 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
希望这个回答能对大家有所帮助。
我正在使用 Spring Boot 2 和 Liquibase (Core 3.6.2),我的数据库是 PostgreSQL。 我正在 db.changelog-master.xml:
中通过这个变更集创建 table<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 文件向此 table 插入值的下一个变更集:
<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 创建了 table 并且当它尝试插入值时我收到此消息:
ERROR: column "id" is of type uuid but expression is of type character varying
问题出在 class ExecutablePreparedStatementBase 中,它位于 liquibase-core 依赖项中,并且此 class 中的方法行造成此错误:
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 来执行查询。问题是因为 table test 的列类型是 uuid,而 liquibase 试图插入 string。如果我们使用 JDBC 手动向 table 插入值,我们应该使用 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
希望这个回答能对大家有所帮助。