Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL Check Constraint in Liquibase

I am wanting to create a check constraint in Liquibase on a PostgreSQL database table for an integer data type column that follows this logic:

int_value >= 0 AND int_value <= 6

What is the proper XML request to make this happen?

like image 921
Brad Richardson Avatar asked Jul 11 '16 19:07

Brad Richardson


2 Answers

This should be the way:

     <column name="int_value" type="INT" >
        <constraints checkConstraint="CHECK (int_value &gt;= 0 AND int_value &lt;= 6)"/>
    </column>

However, current Liquibase (3.5.1) ignores checkConstraint attribute. There is a pull request, but it is added only to 4.0 milestone.

Thus, we have to use the raw sql for check constraints for the time being. This works for me:

<createTable tableName="test">
     <column name="int_value" type="INT"/>
</createTable>
<sql>
    ALTER TABLE test ADD CONSTRAINT int_check CHECK (int_value &gt;=0 AND int_value &lt;= 6)
</sql>
like image 143
Constantine Avatar answered Nov 15 '22 17:11

Constantine


<sql endDelimiter="\nGO">
  ALTER TABLE table_name ADD CONSTRAINT check_name CHECK (int_value &gt;=0 AND int_value &lt;= 6)
</sql>
like image 42
Brad Richardson Avatar answered Nov 15 '22 16:11

Brad Richardson