Skip to main content

ExclusionConstraint

This class enables the creation of exclusion constraints on PostgreSQL databases, ensuring that if any two rows are compared on specified expressions using specified operators, at least one of these operator comparisons returns false. It supports various index types such as GiST, Hash, and SP-GiST, and allows for optional conditions, deferred enforcement, and included columns.

Attributes

AttributeTypeDescription
templatestring = "CONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)s"The SQL template string used to generate the exclusion constraint definition for the database.
expressionslistA list of 2-tuples containing the field names or expressions and their corresponding operators that define the exclusion criteria.
index_typestring = "GIST"The type of index used for the constraint, which must be one of 'GIST', 'HASH', or 'SPGIST'.
condition[Q](../../../db/models/query/utils/q.md?sid=django_db_models_query_utils_q) = nullA Q object that specifies the condition for a partial exclusion constraint.
deferrable[Deferrable](../../../db/models/constraints/deferrable.md?sid=django_db_models_constraints_deferrable) = nullA Deferrable instance used to specify if and when the constraint validation can be deferred until the end of a transaction.
includetuple = ()A tuple of field names to be included in the exclusion constraint as non-key columns for covering indexes.

Constructor

Signature

def ExclusionConstraint(
name: str,
expressions: list,
index_type: str = None,
condition: [Q](../../../db/models/query/utils/q.md?sid=django_db_models_query_utils_q) = None,
deferrable: [Deferrable](../../../db/models/constraints/deferrable.md?sid=django_db_models_constraints_deferrable) = None,
include: list|tuple = None,
violation_error_code: str = None,
violation_error_message: str = None
) - > null

Parameters

NameTypeDescription
namestrThe name of the constraint.
expressionslistA list of 2-tuples containing the expression and the operator.
index_typestr = NoneThe type of index to use (GiST, Hash, or SP-GiST). Defaults to GIST.
condition[Q](../../../db/models/query/utils/q.md?sid=django_db_models_query_utils_q) = NoneA Q object that specifies the condition for the constraint.
deferrable[Deferrable](../../../db/models/constraints/deferrable.md?sid=django_db_models_constraints_deferrable) = NoneWhether the constraint is deferrable.
include`listtuple` = None
violation_error_codestr = NoneThe error code used when ValidationError is raised.
violation_error_messagestr = NoneThe error message used when ValidationError is raised.

Methods


check()

@classmethod
def check(
model: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
connection: [BaseDatabaseWrapper](../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)
) - > list

Performs validation checks on the constraint to ensure all referenced fields exist and are valid for the model.

Parameters

NameTypeDescription
model[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The model class the constraint is being applied to
connection[BaseDatabaseWrapper](../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)The current database connection used for backend-specific checks

Returns

TypeDescription
listA list of error messages or warnings found during the check

constraint_sql()

@classmethod
def constraint_sql(
model: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)
) - > [Statement](../../../db/backends/ddl/references/statement.md?sid=django_db_backends_ddl_references_statement)

Constructs the full SQL statement for the EXCLUDE constraint, including index type, expressions, and optional clauses like WHERE or DEFERRABLE.

Parameters

NameTypeDescription
model[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The model class defining the table for the constraint
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The schema editor used to quote names and generate backend-specific SQL fragments

Returns

TypeDescription
[Statement](../../../db/backends/ddl/references/statement.md?sid=django_db_backends_ddl_references_statement)A Statement object containing the SQL template and parameters for creating the constraint

create_sql()

@classmethod
def create_sql(
model: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)
) - > [Statement](../../../db/backends/ddl/references/statement.md?sid=django_db_backends_ddl_references_statement)

Generates the ALTER TABLE SQL statement required to add the exclusion constraint to the database.

Parameters

NameTypeDescription
model[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The model class whose table will be altered
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The schema editor used to format the table name and constraint SQL

Returns

TypeDescription
[Statement](../../../db/backends/ddl/references/statement.md?sid=django_db_backends_ddl_references_statement)The SQL statement to add the constraint

remove_sql()

@classmethod
def remove_sql(
model: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)
) - > str

Generates the SQL statement required to drop the exclusion constraint from the database table.

Parameters

NameTypeDescription
model[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The model class whose table the constraint is being removed from
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The schema editor used to generate the delete constraint SQL

Returns

TypeDescription
strThe SQL statement to remove the constraint

deconstruct()

@classmethod
def deconstruct() - > tuple

Returns a serialized representation of the constraint, used by Django's migration framework to recreate the instance.

Returns

TypeDescription
tupleA 3-tuple containing the import path, positional arguments, and keyword arguments

validate()

@classmethod
def validate(
model: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
instance: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
exclude: list,
using: str
) - > null

Validates that the given model instance does not violate the exclusion constraint by checking for overlapping records in the database.

Parameters

NameTypeDescription
model[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The model class to validate against
instance[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The specific model instance being validated
excludelistA list of field names to exclude from the validation check
usingstrThe database alias to use for the validation query

Returns

TypeDescription
nullNone