PostgreSQL Check Constraint in Liquibase

笑着哭i 提交于 2019-12-07 08:58:32

问题


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?


回答1:


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>



回答2:


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


来源:https://stackoverflow.com/questions/38315020/postgresql-check-constraint-in-liquibase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!