Liquibase adding default value and not null constraint

liquibase

Solution

There is a difference between the `defautlNullValue` in `addNotNullConstraint` and `defaultValueNumeric` in `addDefaultValue`. Using `addDefaultValue` just sets a default value for future rows inserted into the column but `defaultNullValue` in `addNotNullConstraint` will cause liquibase to generate an additional SQL statement of `update location set rehandle=0 where rehandle is null` to change the value of already existing rows so that a null constraint can be added.

I would think either order would work fine, what exception were you seeing?

Problem

I am bit new to Liquibase. I came across a scenario where in one `changeSet` it is trying to add a default value first and not null constraint next to that. But problem over here is both `<addDefaultValue/>` and `<addNotNullConstraint/>` tags have a default value attributes so eventually I am ending up with an exception. Below is the `changeSet` that I have, ``` <changeSet id="f3047816-2d48-4341-a4ce-deface083cea" author="MineStar" failOnError="true"> <preConditions onFailMessage="Ignored AlterColumn for REHANDLE of table LOCATION as column does not exist or already has a NOT NULL constraint." onFail="MARK_RAN"> <columnExists tableName="LOCATION" columnName="REHANDLE"/> <ext:columnIsNullable tableName="LOCATION" columnName="REHANDLE"/> </preConditions> <comment>AHS-1373: AlterColumn LOCATION.REHANDLE - nullability changed from true to false - defaultValue changed from 'null' to '0'</comment> <addDefaultValue columnName="REHANDLE" columnDataType="BOOLEAN" defaultValueNumeric="0" tableName="LOCATION"/> <addNotNullConstraint columnName="REHANDLE" defaultNullValue="0" columnDataType="BOOLEAN" tableName="LOCATION"/> </changeSet> ``` Here one more strange thing I could see is if I rearrange the order of adding default value and not null constraints tags I won't get any exception that is first adding not null constraint and then default value like below. But I should not do that as it effects checksum in database all that I can do is adding a new `changeSet` to resolve the exception. ``` <addNotNullConstraint columnName="REHANDLE" defaultNullValue="0" columnDataType="BOOLEAN" tableName="LOCATION"/> <addDefaultValue columnName="REHANDLE" columnDataType="BOOLEAN" defaultValueNumeric="0" tableName="LOCATION"/>. ```

Original source

Related problems