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

时间:2018-10-03 15:16:07

标签: java postgresql spring-boot database-migration 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执行查询。问题是因为表 test 的列类型为 uuid ,而liquibase试图插入 string 。而且,如果我们使用JDBC手动向该表中插入值,则应使用setObject的{​​{1}}方法而不是PreparedStatement。但是,如果此问题位于liquibase-core.jar中,该如何解决此问题?有人能帮我吗?

1 个答案:

答案 0 :(得分:3)

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

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

我希望这个答案能对某人有所帮助。