3
votes

I tried with liquibase to set the default value of my column to null with liquibase "addDefaultValue" syntax:

<changeSet id="20181213171501-2">
    <!-- Add default value -->
    <addDefaultValue tableName="myTable"
        columnDataType="boolean"
        columnName="myColumn"
        defaultValueBoolean="null" />
</changeSet>

But inserting a new row to myTable showed that the default value was still set to 'false', as before. So liquibase changeset didn't work.

How to set a column default value to null with liquibase?

2
Removing the line defaultValueBoolean="null" resulted in liquibase.exception.ValidationFailedException: Validation Failed: defaultValue is required, Default value of null does not match defined type of boolean even if the column is nullableRotS

2 Answers

1
votes

The solution I found was to use a raw SQL query instead of liquibase "addDefaultValue" syntax:

<changeSet id="20181213171501-2">
    <!-- Add default value -->
    <sql dbms="mysql">
        ALTER TABLE myTable MODIFY myColumn BOOLEAN NULL DEFAULT NULL
    </sql>
</changeSet>
1
votes

That exactly what the dropDefaultValue refactoring does: it resets the default value of nullable columns to null.

<changeSet id="20181213171501-2">
    <!-- Reset default value to NULL -->
    <dropDefaultValue tableName="myTable" columnName="mColumn" />
</changeSet>