首页
随机页面
分类
查看“Django/docs/3.0.x/ref/contrib/postgres/constraints”的源代码
来自菜鸟教程
Django/docs/3.0.x/ref/contrib/postgres/constraints / ←
PostgreSQL 特定的数据库约束 — Django 文档
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
<div id="module-django.contrib.postgres.constraints" class="section"> <span id="postgresql-specific-database-constraints"></span> = PostgreSQL specific database constraints = PostgreSQL supports additional data integrity constraints available from the <code>django.contrib.postgres.constraints</code> module. They are added in the model [[../../../models/options#django.db.models.Options|<code>Meta.constraints</code>]] option. <div id="exclusionconstraint" class="section"> == <code>ExclusionConstraint</code> == <div class="versionadded"> </div> <dl> <dt>''class'' <code>ExclusionConstraint</code><span class="sig-paren">(</span>''<span class="o">*</span>'', ''<span class="n">name</span>'', ''<span class="n">expressions</span>'', ''<span class="n">index_type</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">condition</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt> <dd><p>Creates an exclusion constraint in the database. Internally, PostgreSQL implements exclusion constraints using indexes. The default index type is [https://www.postgresql.org/docs/current/gist.html GiST]. To use them, you need to activate the [https://www.postgresql.org/docs/current/btree-gist.html btree_gist extension] on PostgreSQL. You can install it using the [[../operations#django.contrib.postgres.operations|<code>BtreeGistExtension</code>]] migration operation.</p> <p>If you attempt to insert a new row that conflicts with an existing row, an [[../../../exceptions#django.db|<code>IntegrityError</code>]] is raised. Similarly, when update conflicts with an existing row.</p></dd></dl> <div id="name" class="section"> === <code>name</code> === ; <code>ExclusionConstraint.</code><code>name</code> : The name of the constraint. </div> <div id="expressions" class="section"> === <code>expressions</code> === ; <code>ExclusionConstraint.</code><code>expressions</code> : An iterable of 2-tuples. The first element is an expression or string. The second element is an SQL operator represented as a string. To avoid typos, you may use [[../fields#django.contrib.postgres.fields|<code>RangeOperators</code>]] which maps the operators with strings. For example: <div class="highlight-default notranslate"> <div class="highlight"> <pre>expressions=[ ('timespan', RangeOperators.ADJACENT_TO), (F('room'), RangeOperators.EQUAL), ]</pre> </div> </div> <div class="admonition-restrictions-on-operators admonition"> Restrictions on operators. Only commutative operators can be used in exclusion constraints. </div> </div> <div id="index-type" class="section"> === <code>index_type</code> === ; <code>ExclusionConstraint.</code><code>index_type</code> : The index type of the constraint. Accepted values are <code>GIST</code> or <code>SPGIST</code>. Matching is case insensitive. If not provided, the default index type is <code>GIST</code>. </div> <div id="condition" class="section"> === <code>condition</code> === ; <code>ExclusionConstraint.</code><code>condition</code> : A [[../../../models/querysets#django.db.models|<code>Q</code>]] object that specifies the condition to restrict a constraint to a subset of rows. For example, <code>condition=Q(cancelled=False)</code>. These conditions have the same database restrictions as [[../../../models/indexes#django.db.models.Index|<code>django.db.models.Index.condition</code>]]. </div> <div id="examples" class="section"> === 示例 === The following example restricts overlapping reservations in the same room, not taking canceled reservations into account: <div class="highlight-default notranslate"> <div class="highlight"> <pre>from django.contrib.postgres.constraints import ExclusionConstraint from django.contrib.postgres.fields import DateTimeRangeField, RangeOperators from django.db import models from django.db.models import Q class Room(models.Model): number = models.IntegerField() class Reservation(models.Model): room = models.ForeignKey('Room', on_delete=models.CASCADE) timespan = DateTimeRangeField() cancelled = models.BooleanField(default=False) class Meta: constraints = [ ExclusionConstraint( name='exclude_overlapping_reservations', expressions=[ ('timespan', RangeOperators.OVERLAPS), ('room', RangeOperators.EQUAL), ], condition=Q(cancelled=False), ), ]</pre> </div> </div> In case your model defines a range using two fields, instead of the native PostgreSQL range types, you should write an expression that uses the equivalent function (e.g. <code>TsTzRange()</code>), and use the delimiters for the field. Most often, the delimiters will be <code>'[)'</code>, meaning that the lower bound is inclusive and the upper bound is exclusive. You may use the [[../fields#django.contrib.postgres.fields|<code>RangeBoundary</code>]] that provides an expression mapping for the [https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-INCLUSIVITY range boundaries]. For example: <div class="highlight-default notranslate"> <div class="highlight"> <pre>from django.contrib.postgres.constraints import ExclusionConstraint from django.contrib.postgres.fields import ( DateTimeRangeField, RangeBoundary, RangeOperators, ) from django.db import models from django.db.models import Func, Q class TsTzRange(Func): function = 'TSTZRANGE' output_field = DateTimeRangeField() class Reservation(models.Model): room = models.ForeignKey('Room', on_delete=models.CASCADE) start = models.DateTimeField() end = models.DateTimeField() cancelled = models.BooleanField(default=False) class Meta: constraints = [ ExclusionConstraint( name='exclude_overlapping_reservations', expressions=( (TsTzRange('start', 'end', RangeBoundary()), RangeOperators.OVERLAPS), ('room', RangeOperators.EQUAL), ), condition=Q(cancelled=False), ), ]</pre> </div> </div> </div> </div> </div> [[Category:Django 3.0.x 中文文档]]
返回至“
PostgreSQL 特定的数据库约束 — Django 文档
”。