Liquibase problem when inserting UUID value to PostgreSQL Database

Abzelhan :

I'm using Spring Boot 2 with Liquibase (Core 3.6.2) and my DB is PostgreSQL. I'm creating table by this changeset in my 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>

The next changeset used to insert values to this table from csv file:

<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>

If i specify type UUID in column id instead of STRING the liquibase will tell me:

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

The content of test.csv file:

"id","note"
"18d892e0-e88d-4b18-a5c0-c209983ea3c0","test-note"

When i run application, liquibase created table and when it try to insert values i get this message:

ERROR: column "id" is of type uuid but expression is of type character varying

The problem is in class ExecutablePreparedStatementBase that is located in liquibase-core dependency, and rows of method in this class that create this error:

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 used JDBC and PreparedStatement for executing queries. The problem is because type of column of table test is uuid, and liquibase tried to insert string. And if we manually insert values to this table by using JDBC we should use setObject method of PreparedStatement instead of setString. But how can i fix this problem if this problem located in liquibase-core.jar? Can someone help me?

Abzelhan :

It was very hurts, but i found solution. You need to specify parameter stringtype=unspecified in your JDBC URL property. For example in application.properties:

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

I hope this answer will help someone.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=94114&siteId=1
Recommended