7

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"/>.
Joey
  • 344,408
  • 85
  • 689
  • 683
Jai
  • 99
  • 1
  • 1
  • 7
  • Why don't you just remove the `defaultNullValue="0"` from the `addNotNullConstraint` tag (or remove the `addDefaultValue` completely)? –  Nov 26 '14 at 07:26
  • No i just can not edit existing changeSet as this has the effect of changing the checksum of the changeset and means that anyone who has already run the old changeset will have problem with this. so all that i can do is creating a new changeSet to solve this and by making existing one remains same. – Jai Nov 26 '14 at 09:04
  • Eventhough if i remove defaultNullValue="0" from the addNotNullConstraint tag it is still throwing the exception. – Jai Nov 26 '14 at 09:08
  • Possible duplicate of [How to add new column with default value from existing column in Liquibase](https://stackoverflow.com/questions/35172172/how-to-add-new-column-with-default-value-from-existing-column-in-liquibase) – axiopisty Apr 22 '19 at 16:41

1 Answers1

8

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?

axiopisty
  • 4,972
  • 8
  • 44
  • 73
Nathan Voxland
  • 15,453
  • 2
  • 48
  • 64