---
title: "PostgreSQL specific database constraints"
version: 3.0
locale: ja
source: https://docs.djangoproject.com/ja/3.0/ref/contrib/postgres/constraints/
canonical: https://djangodocs.dev/ja/3.0/ref/contrib/postgres/constraints/
---
# PostgreSQL specific database constraints

PostgreSQL supports additional data integrity constraints available from the
`django.contrib.postgres.constraints` module. They are added in the model
[`Meta.constraints`](/ja/3.0/ref/models/options/#django.db.models.Options.constraints) option.

## `ExclusionConstraint`

> **New in Django 3.0**

#### `class ExclusionConstraint(* (Keyword-only parameters separator (PEP 3102)), name, expressions, index_type=None, condition=None)`

Creates an exclusion constraint in the database. Internally, PostgreSQL
implements exclusion constraints using indexes. The default index type is
[GiST](https://www.postgresql.org/docs/current/gist.html). To use them,
you need to activate the [btree\_gist extension](https://www.postgresql.org/docs/current/btree-gist.html) on PostgreSQL.
You can install it using the
[`BtreeGistExtension`](/ja/3.0/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGistExtension) migration
operation.

If you attempt to insert a new row that conflicts with an existing row, an
[`IntegrityError`](/ja/3.0/ref/exceptions/#django.db.IntegrityError) is raised. Similarly, when update
conflicts with an existing row.

### `name`

#### `ExclusionConstraint.name`

The name of the constraint.

### `expressions`

#### `ExclusionConstraint.expressions`

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 [`RangeOperators`](/ja/3.0/ref/contrib/postgres/fields/#django.contrib.postgres.fields.RangeOperators) which maps the
operators with strings. For example:

```
expressions=[
    ('timespan', RangeOperators.ADJACENT_TO),
    (F('room'), RangeOperators.EQUAL),
]
```

> **Restrictions on operators.**
>
> Only commutative operators can be used in exclusion constraints.

### `index_type`

#### `ExclusionConstraint.index_type`

The index type of the constraint. Accepted values are `GIST` or `SPGIST`.
Matching is case insensitive. If not provided, the default index type is
`GIST`.

### `condition`

#### `ExclusionConstraint.condition`

A [`Q`](/ja/3.0/ref/models/querysets/#django.db.models.Q) object that specifies the condition to restrict
a constraint to a subset of rows. For example,
`condition=Q(cancelled=False)`.

These conditions have the same database restrictions as
[`django.db.models.Index.condition`](/ja/3.0/ref/models/indexes/#django.db.models.Index.condition).

### 例

The following example restricts overlapping reservations in the same room, not
taking canceled reservations into account:

```
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),
            ),
        ]
```

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. `TsTzRange()`), and use the delimiters for the field. Most
often, the delimiters will be `'[)'`, meaning that the lower bound is
inclusive and the upper bound is exclusive. You may use the
[`RangeBoundary`](/ja/3.0/ref/contrib/postgres/fields/#django.contrib.postgres.fields.RangeBoundary) that provides an
expression mapping for the [range boundaries](https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-INCLUSIVITY). For example:

```
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),
            ),
        ]
```
