RotS
RotS

Reputation: 2410

Liquibase set column default value to null

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?

Upvotes: 5

Views: 13361

Answers (2)

Chris
Chris

Reputation: 4231

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>

Upvotes: 3

RotS
RotS

Reputation: 2410

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>

Upvotes: 1

Related Questions