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 option.

ExclusionConstraint

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

Creates an exclusion constraint in the database. Internally, PostgreSQL implements exclusion constraints using indexes. The default index type is GiST. To use them, you need to activate the btree_gist extension on PostgreSQL. You can install it using the BtreeGistExtension migration operation.

If you attempt to insert a new row that conflicts with an existing row, an IntegrityError is raised. Similarly, when update conflicts with an existing row.

Exclusion constraints are checked during the model validation.

Changed in Django 4.1:

In older versions, exclusion constraints were not checked during model validation.

name

ExclusionConstraint.name

See BaseConstraint.name.

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 which maps the operators with strings. For example:

  1. expressions = [
  2. ("timespan", RangeOperators.ADJACENT_TO),
  3. (F("room"), RangeOperators.EQUAL),
  4. ]

Restrictions on operators.

Only commutative operators can be used in exclusion constraints.

The OpClass() expression can be used to specify a custom operator class for the constraint expressions. For example:

  1. expressions = [
  2. (OpClass("circle", name="circle_ops"), RangeOperators.OVERLAPS),
  3. ]

creates an exclusion constraint on circle using circle_ops.

Changed in Django 4.1:

Support for the OpClass() expression was added.

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

deferrable

ExclusionConstraint.deferrable

Set this parameter to create a deferrable exclusion constraint. Accepted values are Deferrable.DEFERRED or Deferrable.IMMEDIATE. For example:

  1. from django.contrib.postgres.constraints import ExclusionConstraint
  2. from django.contrib.postgres.fields import RangeOperators
  3. from django.db.models import Deferrable
  4. ExclusionConstraint(
  5. name="exclude_overlapping_deferred",
  6. expressions=[
  7. ("timespan", RangeOperators.OVERLAPS),
  8. ],
  9. deferrable=Deferrable.DEFERRED,
  10. )

By default constraints are not deferred. A deferred constraint will not be enforced until the end of the transaction. An immediate constraint will be enforced immediately after every command.

Warning

Deferred exclusion constraints may lead to a performance penalty.

include

ExclusionConstraint.include

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 (include) and filter only by indexed fields (expressions).

include is supported for GiST indexes. PostgreSQL 14+ also supports include for SP-GiST indexes.

Changed in Django 4.1:

Support for covering exclusion constraints using SP-GiST indexes on PostgreSQL 14+ was added.

opclasses

ExclusionConstraint.opclasses

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

For example:

  1. ExclusionConstraint(
  2. name="exclude_overlapping_opclasses",
  3. expressions=[("circle", RangeOperators.OVERLAPS)],
  4. opclasses=["circle_ops"],
  5. )

creates an exclusion constraint on circle using circle_ops.

Deprecated since version 4.1: The opclasses parameter is deprecated in favor of using OpClass() in expressions.

violation_error_message

New in Django 4.1.

The error message used when ValidationError is raised during model validation. Defaults to BaseConstraint.violation_error_message.

Examples

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

  1. from django.contrib.postgres.constraints import ExclusionConstraint
  2. from django.contrib.postgres.fields import DateTimeRangeField, RangeOperators
  3. from django.db import models
  4. from django.db.models import Q
  5. class Room(models.Model):
  6. number = models.IntegerField()
  7. class Reservation(models.Model):
  8. room = models.ForeignKey("Room", on_delete=models.CASCADE)
  9. timespan = DateTimeRangeField()
  10. cancelled = models.BooleanField(default=False)
  11. class Meta:
  12. constraints = [
  13. ExclusionConstraint(
  14. name="exclude_overlapping_reservations",
  15. expressions=[
  16. ("timespan", RangeOperators.OVERLAPS),
  17. ("room", RangeOperators.EQUAL),
  18. ],
  19. condition=Q(cancelled=False),
  20. ),
  21. ]

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 that provides an expression mapping for the range boundaries. For example:

  1. from django.contrib.postgres.constraints import ExclusionConstraint
  2. from django.contrib.postgres.fields import (
  3. DateTimeRangeField,
  4. RangeBoundary,
  5. RangeOperators,
  6. )
  7. from django.db import models
  8. from django.db.models import Func, Q
  9. class TsTzRange(Func):
  10. function = "TSTZRANGE"
  11. output_field = DateTimeRangeField()
  12. class Reservation(models.Model):
  13. room = models.ForeignKey("Room", on_delete=models.CASCADE)
  14. start = models.DateTimeField()
  15. end = models.DateTimeField()
  16. cancelled = models.BooleanField(default=False)
  17. class Meta:
  18. constraints = [
  19. ExclusionConstraint(
  20. name="exclude_overlapping_reservations",
  21. expressions=[
  22. (
  23. TsTzRange("start", "end", RangeBoundary()),
  24. RangeOperators.OVERLAPS,
  25. ),
  26. ("room", RangeOperators.EQUAL),
  27. ],
  28. condition=Q(cancelled=False),
  29. ),
  30. ]