本文介绍了Liquibase中的PostgreSQL检查约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PostgreSQL数据库表的Liquibase中为遵循此逻辑的整数数据类型列创建检查约束:

int_value >= 0 AND int_value <= 6

要实现此目的,正确的XML请求是什么?

解决方案

这应该是这样的:

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

但是,当前的Liquibase(3.5.1)忽略checkConstraint属性.有一个拉动请求,但仅添加到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>

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

这篇关于Liquibase中的PostgreSQL检查约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 06:36