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
| Attribute | Type | Description |
|---|---|---|
| template | string = "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. |
| expressions | list | A list of 2-tuples containing the field names or expressions and their corresponding operators that define the exclusion criteria. |
| index_type | string = "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) = null | A Q object that specifies the condition for a partial exclusion constraint. |
| deferrable | [Deferrable](../../../db/models/constraints/deferrable.md?sid=django_db_models_constraints_deferrable) = null | A Deferrable instance used to specify if and when the constraint validation can be deferred until the end of a transaction. |
| include | tuple = () | 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
| Name | Type | Description |
|---|---|---|
| name | str | The name of the constraint. |
| expressions | list | A list of 2-tuples containing the expression and the operator. |
| index_type | str = None | The 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) = None | A Q object that specifies the condition for the constraint. |
| deferrable | [Deferrable](../../../db/models/constraints/deferrable.md?sid=django_db_models_constraints_deferrable) = None | Whether the constraint is deferrable. |
| include | `list | tuple` = None |
| violation_error_code | str = None | The error code used when ValidationError is raised. |
| violation_error_message | str = None | The 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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
str | The 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
| Type | Description |
|---|---|
tuple | A 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
| Name | Type | Description |
|---|---|---|
| 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 |
| exclude | list | A list of field names to exclude from the validation check |
| using | str | The database alias to use for the validation query |
Returns
| Type | Description |
|---|---|
null | None |