PostgreSQL Check Constraint in Liquibase

时间:2016-07-11 19:47:36

标签: postgresql 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?

2 个答案:

答案 0 :(得分:6)

这应该是这样的:

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

但是,当前的Liquibase(3.5.1)会忽略checkConstraint属性。有一个pull request,但它只添加到4.0里程碑。

因此,我们必须暂时使用原始sql进行检查约束。这对我有用:

<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>

答案 1 :(得分:1)

<sql endDelimiter="\nGO">
  ALTER TABLE table_name ADD CONSTRAINT check_name CHECK (int_value &gt;=0 AND int_value &lt;= 6)
</sql>