Zatosssas
Zatosssas

Reputation: 81

Liquibase "modifyDataType" tag always makes the column nullable

When I do-

<modifyDataType 
    tableName="EMCostUnit" 
    columnName="GUID" 
    newDataType="VARCHAR(50)"/>

In liquibase, it always resets the nullable constraints. Is it possible to add some parameter so it didn't remove the constraints, because now after each modifyDataType, I need additional changeSet to add back not null constraints.

Upvotes: 8

Views: 7465

Answers (2)

Vinayaka S P
Vinayaka S P

Reputation: 305

The answer by @orikosaki works. However, as per the documentation of Liquibase, newDataType field will Only modifies the data type itself and cannot define constraints

I recommend adding addNotNullConstraint right after the modifyDataType just to be safe and ensure the not null constraint gets added.

Example (in yaml):

databaseChangeLog:
  - changeSet:
      id: 1
      author: your_name
      changes:
        - modifyDataType:
            tableName: EMCostUnit
            columnName: GUID
            newDataType: nvarchar(50)

  - changeSet:
      id: 2
      author: your_name
      changes:
        - addNotNullConstraint:
            tableName: EMCostUnit
            columnName: GUID
            columnDataType: nvarchar(max)
            constraintName: const-name

in xml:

<changeSet  author="your-name"  id=1>  
    <modifyDataType 
            columnName="GUID"  
            newDataType="nvarchar(50)"
            tableName="EMCostUnit"/>  
</changeSet>
<changeSet author="your-name"  id=2>  
    <addNotNullConstraint
           columnName="GUID"  
           columnDataType="nvarchar(50)"  
           constraintName="const-name"  
           tableName="EMCostUnit"/> 
</changeSet>

Note that in addNotNullConstraint, the property columnDataType is required for mssql, mysql, informix, and mariadb.

Upvotes: 1

orikosaki
orikosaki

Reputation: 186

According to the issue you encountered - you must be using MySQL DBMS.

If it is NOT NULL constraint you are referring to, it is possible to add it as the part of the newDataType-

<modifyDataType 
    tableName="EMCostUnit" 
    columnName="GUID" 
    newDataType="VARCHAR(50) NOT NULL"/>

Generated SQL would be: (checked on MySQL)

ALTER TABLE EMCostUnit MODIFY GUID VARCHAR(50) NOT NULL;

Upvotes: 9

Related Questions