代码之家  ›  专栏  ›  技术社区  ›  Abzelhan

向PostgreSQL数据库插入uuid值时的liquibase问题

  •  3
  • Abzelhan  · 技术社区  · 6 年前

    我使用的是SpringBoot2和Liquibase(核心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>
    

    如果我在列中指定uuid类型 身份证件 Liquibase将告诉我:

    loadData type of uuid is not supported. Please use BOOLEAN, NUMERIC, DATE, STRING, COMPUTED or SKIP
    

    内容 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核心依赖项中,并且该类中创建此错误的方法行:

    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手动向这个表中插入值,我们应该使用 setObject 方法 PreparedStatement 而不是 setString . 但是如果这个问题位于liquibase-core.jar中,我该如何解决这个问题呢?有人能帮我吗?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Abzelhan    6 年前

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

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

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