summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/constraints.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/contrib/postgres/constraints.py')
-rw-r--r--django/contrib/postgres/constraints.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py
index aea1f9bb3d..cdb238c405 100644
--- a/django/contrib/postgres/constraints.py
+++ b/django/contrib/postgres/constraints.py
@@ -1,3 +1,4 @@
+from django.db import NotSupportedError
from django.db.backends.ddl_references import Statement, Table
from django.db.models import Deferrable, F, Q
from django.db.models.constraints import BaseConstraint
@@ -7,11 +8,11 @@ __all__ = ['ExclusionConstraint']
class ExclusionConstraint(BaseConstraint):
- template = 'CONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(where)s%(deferrable)s'
+ template = 'CONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)s'
def __init__(
self, *, name, expressions, index_type=None, condition=None,
- deferrable=None,
+ deferrable=None, include=None,
):
if index_type and index_type.lower() not in {'gist', 'spgist'}:
raise ValueError(
@@ -39,10 +40,19 @@ class ExclusionConstraint(BaseConstraint):
raise ValueError(
'ExclusionConstraint.deferrable must be a Deferrable instance.'
)
+ if not isinstance(include, (type(None), list, tuple)):
+ raise ValueError(
+ 'ExclusionConstraint.include must be a list or tuple.'
+ )
+ if include and index_type and index_type.lower() != 'gist':
+ raise ValueError(
+ 'Covering exclusion constraints only support GiST indexes.'
+ )
self.expressions = expressions
self.index_type = index_type or 'GIST'
self.condition = condition
self.deferrable = deferrable
+ self.include = tuple(include) if include else ()
super().__init__(name=name)
def _get_expression_sql(self, compiler, connection, query):
@@ -67,15 +77,18 @@ class ExclusionConstraint(BaseConstraint):
compiler = query.get_compiler(connection=schema_editor.connection)
expressions = self._get_expression_sql(compiler, schema_editor.connection, query)
condition = self._get_condition_sql(compiler, schema_editor, query)
+ include = [model._meta.get_field(field_name).column for field_name in self.include]
return self.template % {
'name': schema_editor.quote_name(self.name),
'index_type': self.index_type,
'expressions': ', '.join(expressions),
+ 'include': schema_editor._index_include_sql(model, include),
'where': ' WHERE (%s)' % condition if condition else '',
'deferrable': schema_editor._deferrable_constraint_sql(self.deferrable),
}
def create_sql(self, model, schema_editor):
+ self.check_supported(schema_editor)
return Statement(
'ALTER TABLE %(table)s ADD %(constraint)s',
table=Table(model._meta.db_table, schema_editor.quote_name),
@@ -89,6 +102,12 @@ class ExclusionConstraint(BaseConstraint):
schema_editor.quote_name(self.name),
)
+ def check_supported(self, schema_editor):
+ if self.include and not schema_editor.connection.features.supports_covering_gist_indexes:
+ raise NotSupportedError(
+ 'Covering exclusion constraints requires PostgreSQL 12+.'
+ )
+
def deconstruct(self):
path, args, kwargs = super().deconstruct()
kwargs['expressions'] = self.expressions
@@ -98,6 +117,8 @@ class ExclusionConstraint(BaseConstraint):
kwargs['index_type'] = self.index_type
if self.deferrable:
kwargs['deferrable'] = self.deferrable
+ if self.include:
+ kwargs['include'] = self.include
return path, args, kwargs
def __eq__(self, other):
@@ -107,15 +128,17 @@ class ExclusionConstraint(BaseConstraint):
self.index_type == other.index_type and
self.expressions == other.expressions and
self.condition == other.condition and
- self.deferrable == other.deferrable
+ self.deferrable == other.deferrable and
+ self.include == other.include
)
return super().__eq__(other)
def __repr__(self):
- return '<%s: index_type=%s, expressions=%s%s%s>' % (
+ return '<%s: index_type=%s, expressions=%s%s%s%s>' % (
self.__class__.__qualname__,
self.index_type,
self.expressions,
'' if self.condition is None else ', condition=%s' % self.condition,
'' if self.deferrable is None else ', deferrable=%s' % self.deferrable,
+ '' if not self.include else ', include=%s' % repr(self.include),
)