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?
This should be the way:
<column name="int_value" type="INT" >
<constraints checkConstraint="CHECK (int_value >= 0 AND int_value <= 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 >=0 AND int_value <= 6)
</sql>
<sql endDelimiter="\nGO">
ALTER TABLE table_name ADD CONSTRAINT check_name CHECK (int_value >=0 AND int_value <= 6)
</sql>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With