PostgreSQL 特有数据库约束

    class ExclusionConstraint(**, name, expressions, index_type=None, condition=None, deferrable=None, include=None, opclasses=()*)

    在数据库中创建一个排除约束。在内部,PostgreSQL 使用索引来实现排除约束的功能。默认的索引类型是 。要使用它们,你需要激活 PostgreSQL 上的 btree_gist 扩展 。你可以使用 迁移操作来安装它。

    如果你试图插入一条新的记录,而这条记录与现有的记录发生冲突,就会引发一个 IntegrityError。同样,当更新与现有的记录冲突时,也会发出 。

    ExclusionConstraint.``name

    约束的名称。

    expressions

    ExclusionConstraint.``expressions

    一个二元元组的迭代。第一个元素是一个表达式或字符串。第二个元素是一个用字符串表示的 SQL 操作符。为了避免错别字,你可以使用 RangeOperators 将操作符与字符串进行映射。例如:

    对操作的限制。

    只有交换运算符才能用于排除约束。

    限制因素的索引类型。接受的值是 GISTSPGIST。匹配是不分大小写的。如果没有提供,默认索引类型是 GIST

    condition

    ExclusionConstraint.``condition

    一个 对象,用于指定将约束条件限制在行的子集上。例如,condition=Q(cancelled=False)

    这些条件与 django.db.models.Index.condition 具有相同的数据库限制。

    ExclusionConstraint.``deferrable

    New in Django 3.1.

    设置这一参数,可创建一个可推迟的排除限制。接受的值是 Deferrable.DEFERREDDeferrable.IMMEDIATE。例如:

    默认情况下,约束条件是不推迟的。推迟的约束条件在事务结束前不会被强制执行。即时约束将在每条命令后立即执行。

    警告

    include

    ExclusionConstraint.``include

    New in Django Development version.

    A list or tuple of the names of the fields to be included in the covering exclusion constraint as non-key columns. This allows index-only scans to be used for queries that select only included fields () and filter only by indexed fields (expressions).

    include is supported only for GiST indexes on PostgreSQL 12+.

    ExclusionConstraint.``opclasses

    New in Django Development version.

    The names of the to use for this constraint. If you require a custom operator class, you must provide one for each expression in the constraint.

    例子:

    creates an exclusion constraint on circle using circle_ops.

    例子

    如果你的模型使用两个字段定义了一个范围,而不是原生的 PostgreSQL 范围类型,你应该写一个使用等价函数的表达式(例如 TsTzRange()),并使用字段的定界符。大多数情况下,定界符将是 '[)',这意味着下界是包含性的,上界是排他性的。你可以使用 ,它为 提供了一个表达式映射。例如: