2018-08-27 10:25:06 +08:00
|
|
|
from enum import Enum
|
|
|
|
|
2021-02-07 03:45:54 +08:00
|
|
|
from django.db.models.expressions import ExpressionList, F
|
|
|
|
from django.db.models.indexes import IndexExpression
|
2018-12-28 03:21:59 +08:00
|
|
|
from django.db.models.query_utils import Q
|
2016-11-05 21:12:12 +08:00
|
|
|
from django.db.models.sql.query import Query
|
|
|
|
|
2018-08-27 10:25:06 +08:00
|
|
|
__all__ = ['CheckConstraint', 'Deferrable', 'UniqueConstraint']
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
|
2018-08-06 10:12:27 +08:00
|
|
|
class BaseConstraint:
|
|
|
|
def __init__(self, name):
|
2016-11-05 21:12:12 +08:00
|
|
|
self.name = name
|
|
|
|
|
2021-02-07 03:45:54 +08:00
|
|
|
@property
|
|
|
|
def contains_expressions(self):
|
|
|
|
return False
|
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
def constraint_sql(self, model, schema_editor):
|
2018-08-06 10:12:27 +08:00
|
|
|
raise NotImplementedError('This method must be implemented by a subclass.')
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
2019-01-01 22:39:58 +08:00
|
|
|
raise NotImplementedError('This method must be implemented by a subclass.')
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def remove_sql(self, model, schema_editor):
|
2019-01-01 22:39:58 +08:00
|
|
|
raise NotImplementedError('This method must be implemented by a subclass.')
|
2016-11-05 21:12:12 +08:00
|
|
|
|
2018-08-06 10:12:27 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
|
|
|
|
path = path.replace('django.db.models.constraints', 'django.db.models')
|
|
|
|
return (path, (), {'name': self.name})
|
|
|
|
|
|
|
|
def clone(self):
|
|
|
|
_, args, kwargs = self.deconstruct()
|
|
|
|
return self.__class__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class CheckConstraint(BaseConstraint):
|
|
|
|
def __init__(self, *, check, name):
|
|
|
|
self.check = check
|
2019-11-15 13:08:36 +08:00
|
|
|
if not getattr(check, 'conditional', False):
|
|
|
|
raise TypeError(
|
|
|
|
'CheckConstraint.check must be a Q instance or boolean '
|
|
|
|
'expression.'
|
|
|
|
)
|
2018-08-06 10:12:27 +08:00
|
|
|
super().__init__(name)
|
|
|
|
|
2019-01-01 22:39:58 +08:00
|
|
|
def _get_check_sql(self, model, schema_editor):
|
2019-09-10 11:58:29 +08:00
|
|
|
query = Query(model=model, alias_cols=False)
|
2018-08-06 10:12:27 +08:00
|
|
|
where = query.build_where(self.check)
|
2019-01-01 22:39:58 +08:00
|
|
|
compiler = query.get_compiler(connection=schema_editor.connection)
|
|
|
|
sql, params = where.as_sql(compiler, schema_editor.connection)
|
|
|
|
return sql % tuple(schema_editor.quote_value(p) for p in params)
|
|
|
|
|
|
|
|
def constraint_sql(self, model, schema_editor):
|
|
|
|
check = self._get_check_sql(model, schema_editor)
|
|
|
|
return schema_editor._check_sql(self.name, check)
|
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
|
|
|
check = self._get_check_sql(model, schema_editor)
|
|
|
|
return schema_editor._create_check_sql(model, self.name, check)
|
|
|
|
|
|
|
|
def remove_sql(self, model, schema_editor):
|
|
|
|
return schema_editor._delete_check_sql(model, self.name)
|
2018-08-06 10:12:27 +08:00
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
def __repr__(self):
|
2021-02-19 19:16:24 +08:00
|
|
|
return '<%s: check=%s name=%s>' % (
|
|
|
|
self.__class__.__qualname__,
|
|
|
|
self.check,
|
|
|
|
repr(self.name),
|
|
|
|
)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2019-09-03 10:09:31 +08:00
|
|
|
if isinstance(other, CheckConstraint):
|
|
|
|
return self.name == other.name and self.check == other.check
|
|
|
|
return super().__eq__(other)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2018-08-06 10:12:27 +08:00
|
|
|
path, args, kwargs = super().deconstruct()
|
|
|
|
kwargs['check'] = self.check
|
|
|
|
return path, args, kwargs
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
|
2018-08-27 10:25:06 +08:00
|
|
|
class Deferrable(Enum):
|
|
|
|
DEFERRED = 'deferred'
|
|
|
|
IMMEDIATE = 'immediate'
|
|
|
|
|
2021-07-12 17:46:37 +08:00
|
|
|
# A similar format was proposed for Python 3.10.
|
|
|
|
def __repr__(self):
|
|
|
|
return f'{self.__class__.__qualname__}.{self._name_}'
|
2021-04-07 16:28:40 +08:00
|
|
|
|
2018-08-27 10:25:06 +08:00
|
|
|
|
2018-08-06 10:30:44 +08:00
|
|
|
class UniqueConstraint(BaseConstraint):
|
2020-06-12 03:37:12 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-02-07 03:45:54 +08:00
|
|
|
*expressions,
|
|
|
|
fields=(),
|
|
|
|
name=None,
|
2020-06-12 03:37:12 +08:00
|
|
|
condition=None,
|
|
|
|
deferrable=None,
|
|
|
|
include=None,
|
|
|
|
opclasses=(),
|
|
|
|
):
|
2021-02-07 03:45:54 +08:00
|
|
|
if not name:
|
|
|
|
raise ValueError('A unique constraint must be named.')
|
|
|
|
if not expressions and not fields:
|
|
|
|
raise ValueError(
|
|
|
|
'At least one field or expression is required to define a '
|
|
|
|
'unique constraint.'
|
|
|
|
)
|
|
|
|
if expressions and fields:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint.fields and expressions are mutually exclusive.'
|
|
|
|
)
|
2018-12-28 03:21:59 +08:00
|
|
|
if not isinstance(condition, (type(None), Q)):
|
|
|
|
raise ValueError('UniqueConstraint.condition must be a Q instance.')
|
2018-08-27 10:25:06 +08:00
|
|
|
if condition and deferrable:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint with conditions cannot be deferred.'
|
|
|
|
)
|
2020-06-29 12:41:23 +08:00
|
|
|
if include and deferrable:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint with include fields cannot be deferred.'
|
|
|
|
)
|
|
|
|
if opclasses and deferrable:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint with opclasses cannot be deferred.'
|
|
|
|
)
|
2021-02-07 03:45:54 +08:00
|
|
|
if expressions and deferrable:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint with expressions cannot be deferred.'
|
|
|
|
)
|
|
|
|
if expressions and opclasses:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint.opclasses cannot be used with expressions. '
|
|
|
|
'Use django.contrib.postgres.indexes.OpClass() instead.'
|
|
|
|
)
|
2018-08-27 10:25:06 +08:00
|
|
|
if not isinstance(deferrable, (type(None), Deferrable)):
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint.deferrable must be a Deferrable instance.'
|
|
|
|
)
|
2019-10-31 20:33:53 +08:00
|
|
|
if not isinstance(include, (type(None), list, tuple)):
|
|
|
|
raise ValueError('UniqueConstraint.include must be a list or tuple.')
|
2020-06-12 03:37:12 +08:00
|
|
|
if not isinstance(opclasses, (list, tuple)):
|
|
|
|
raise ValueError('UniqueConstraint.opclasses must be a list or tuple.')
|
|
|
|
if opclasses and len(fields) != len(opclasses):
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint.fields and UniqueConstraint.opclasses must '
|
|
|
|
'have the same number of elements.'
|
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
self.fields = tuple(fields)
|
2018-12-28 03:21:59 +08:00
|
|
|
self.condition = condition
|
2018-08-27 10:25:06 +08:00
|
|
|
self.deferrable = deferrable
|
2019-10-31 20:33:53 +08:00
|
|
|
self.include = tuple(include) if include else ()
|
2020-06-12 03:37:12 +08:00
|
|
|
self.opclasses = opclasses
|
2021-02-07 03:45:54 +08:00
|
|
|
self.expressions = tuple(
|
|
|
|
F(expression) if isinstance(expression, str) else expression
|
|
|
|
for expression in expressions
|
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
super().__init__(name)
|
|
|
|
|
2021-02-07 03:45:54 +08:00
|
|
|
@property
|
|
|
|
def contains_expressions(self):
|
|
|
|
return bool(self.expressions)
|
|
|
|
|
2018-12-28 03:21:59 +08:00
|
|
|
def _get_condition_sql(self, model, schema_editor):
|
|
|
|
if self.condition is None:
|
|
|
|
return None
|
2019-09-10 11:58:29 +08:00
|
|
|
query = Query(model=model, alias_cols=False)
|
2018-12-28 03:21:59 +08:00
|
|
|
where = query.build_where(self.condition)
|
|
|
|
compiler = query.get_compiler(connection=schema_editor.connection)
|
|
|
|
sql, params = where.as_sql(compiler, schema_editor.connection)
|
|
|
|
return sql % tuple(schema_editor.quote_value(p) for p in params)
|
|
|
|
|
2021-02-07 03:45:54 +08:00
|
|
|
def _get_index_expressions(self, model, schema_editor):
|
|
|
|
if not self.expressions:
|
|
|
|
return None
|
|
|
|
index_expressions = []
|
|
|
|
for expression in self.expressions:
|
|
|
|
index_expression = IndexExpression(expression)
|
|
|
|
index_expression.set_wrapper_classes(schema_editor.connection)
|
|
|
|
index_expressions.append(index_expression)
|
|
|
|
return ExpressionList(*index_expressions).resolve_expression(
|
|
|
|
Query(model, alias_cols=False),
|
|
|
|
)
|
|
|
|
|
2018-08-06 10:30:44 +08:00
|
|
|
def constraint_sql(self, model, schema_editor):
|
2021-05-28 03:35:24 +08:00
|
|
|
fields = [model._meta.get_field(field_name) for field_name in self.fields]
|
2019-10-31 20:33:53 +08:00
|
|
|
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
2018-12-28 03:21:59 +08:00
|
|
|
condition = self._get_condition_sql(model, schema_editor)
|
2021-02-07 03:45:54 +08:00
|
|
|
expressions = self._get_index_expressions(model, schema_editor)
|
2018-08-27 10:25:06 +08:00
|
|
|
return schema_editor._unique_sql(
|
|
|
|
model, fields, self.name, condition=condition,
|
2019-10-31 20:33:53 +08:00
|
|
|
deferrable=self.deferrable, include=include,
|
2021-02-07 03:45:54 +08:00
|
|
|
opclasses=self.opclasses, expressions=expressions,
|
2018-08-27 10:25:06 +08:00
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
2021-05-28 03:35:24 +08:00
|
|
|
fields = [model._meta.get_field(field_name) for field_name in self.fields]
|
2019-10-31 20:33:53 +08:00
|
|
|
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
2018-12-28 03:21:59 +08:00
|
|
|
condition = self._get_condition_sql(model, schema_editor)
|
2021-02-07 03:45:54 +08:00
|
|
|
expressions = self._get_index_expressions(model, schema_editor)
|
2018-08-27 10:25:06 +08:00
|
|
|
return schema_editor._create_unique_sql(
|
|
|
|
model, fields, self.name, condition=condition,
|
2019-10-31 20:33:53 +08:00
|
|
|
deferrable=self.deferrable, include=include,
|
2021-02-07 03:45:54 +08:00
|
|
|
opclasses=self.opclasses, expressions=expressions,
|
2018-08-27 10:25:06 +08:00
|
|
|
)
|
2019-01-01 22:39:58 +08:00
|
|
|
|
|
|
|
def remove_sql(self, model, schema_editor):
|
2018-12-28 03:21:59 +08:00
|
|
|
condition = self._get_condition_sql(model, schema_editor)
|
2019-10-31 20:33:53 +08:00
|
|
|
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
2021-02-07 03:45:54 +08:00
|
|
|
expressions = self._get_index_expressions(model, schema_editor)
|
2018-08-27 10:25:06 +08:00
|
|
|
return schema_editor._delete_unique_sql(
|
|
|
|
model, self.name, condition=condition, deferrable=self.deferrable,
|
2021-02-07 03:45:54 +08:00
|
|
|
include=include, opclasses=self.opclasses, expressions=expressions,
|
2018-08-27 10:25:06 +08:00
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2021-02-07 03:45:54 +08:00
|
|
|
return '<%s:%s%s%s%s%s%s%s>' % (
|
2021-02-19 19:16:24 +08:00
|
|
|
self.__class__.__qualname__,
|
2021-02-07 03:45:54 +08:00
|
|
|
'' if not self.fields else ' fields=%s' % repr(self.fields),
|
|
|
|
'' if not self.expressions else ' expressions=%s' % repr(self.expressions),
|
|
|
|
' name=%s' % repr(self.name),
|
2018-12-28 03:21:59 +08:00
|
|
|
'' if self.condition is None else ' condition=%s' % self.condition,
|
2021-04-07 16:28:40 +08:00
|
|
|
'' if self.deferrable is None else ' deferrable=%r' % self.deferrable,
|
2019-10-31 20:33:53 +08:00
|
|
|
'' if not self.include else ' include=%s' % repr(self.include),
|
2020-06-12 03:37:12 +08:00
|
|
|
'' if not self.opclasses else ' opclasses=%s' % repr(self.opclasses),
|
2018-12-28 03:21:59 +08:00
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2019-09-03 10:09:31 +08:00
|
|
|
if isinstance(other, UniqueConstraint):
|
|
|
|
return (
|
|
|
|
self.name == other.name and
|
|
|
|
self.fields == other.fields and
|
2018-08-27 10:25:06 +08:00
|
|
|
self.condition == other.condition and
|
2019-10-31 20:33:53 +08:00
|
|
|
self.deferrable == other.deferrable and
|
2020-06-12 03:37:12 +08:00
|
|
|
self.include == other.include and
|
2021-02-07 03:45:54 +08:00
|
|
|
self.opclasses == other.opclasses and
|
|
|
|
self.expressions == other.expressions
|
2019-09-03 10:09:31 +08:00
|
|
|
)
|
|
|
|
return super().__eq__(other)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
2021-02-07 03:45:54 +08:00
|
|
|
if self.fields:
|
|
|
|
kwargs['fields'] = self.fields
|
2018-12-28 03:21:59 +08:00
|
|
|
if self.condition:
|
|
|
|
kwargs['condition'] = self.condition
|
2018-08-27 10:25:06 +08:00
|
|
|
if self.deferrable:
|
|
|
|
kwargs['deferrable'] = self.deferrable
|
2019-10-31 20:33:53 +08:00
|
|
|
if self.include:
|
|
|
|
kwargs['include'] = self.include
|
2020-06-12 03:37:12 +08:00
|
|
|
if self.opclasses:
|
|
|
|
kwargs['opclasses'] = self.opclasses
|
2021-02-07 03:45:54 +08:00
|
|
|
return path, self.expressions, kwargs
|