Skip to main content

AddConstraintNotValid

Add a table constraint without enforcing validation, using PostgreSQL's NOT VALID syntax.

Attributes

AttributeTypeDescription
category[OperationCategory](../../../db/migrations/operations/base/operationcategory.md?sid=django_db_migrations_operations_base_operationcategory) = OperationCategory.ADDITIONThe classification of the database operation, set to ADDITION to indicate this class performs a schema addition.

Constructor

Signature

def AddConstraintNotValid(
model_name: string,
constraint: [CheckConstraint](../../../db/models/constraints/checkconstraint.md?sid=django_db_models_constraints_checkconstraint)
)

Parameters

NameTypeDescription
model_namestringThe name of the model the constraint is being added to.
constraint[CheckConstraint](../../../db/models/constraints/checkconstraint.md?sid=django_db_models_constraints_checkconstraint)The check constraint object to be added to the table.

Signature

def AddConstraintNotValid(
model_name: string,
constraint: [CheckConstraint](../../../db/models/constraints/checkconstraint.md?sid=django_db_models_constraints_checkconstraint)
) - > null

Parameters

NameTypeDescription
model_namestringThe name of the database model to which the constraint will be applied.
constraint[CheckConstraint](../../../db/models/constraints/checkconstraint.md?sid=django_db_models_constraints_checkconstraint)The specific check constraint object to be added; must be an instance of CheckConstraint.

Methods


describe()

@classmethod
def describe() - > string

Returns a human-readable string describing the operation for use in migration logs and console output.

Returns

TypeDescription
stringA formatted string indicating the creation of a 'not valid' constraint and the target model name.

database_forwards()

@classmethod
def database_forwards(
app_label: string,
schema_editor: [BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor),
from_state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate),
to_state: [ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)
) - > null

Executes the SQL command to add the constraint to the database using the 'NOT VALID' syntax to skip initial validation of existing rows.

Parameters

NameTypeDescription
app_labelstringThe label of the application the migration belongs to.
schema_editor[BaseDatabaseSchemaEditor](../../../db/backends/base/schema/basedatabaseschemaeditor.md?sid=django_db_backends_base_schema_basedatabaseschemaeditor)The database abstraction layer used to execute SQL statements.
from_state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The state of the project before the migration is applied.
to_state[ProjectState](../../../db/migrations/state/projectstate.md?sid=django_db_migrations_state_projectstate)The state of the project after the migration is applied.

Returns

TypeDescription
nullNone

migration_name_fragment()

@classmethod
def migration_name_fragment() - > string

Generates a string fragment used to automatically name the migration file containing this operation.

Returns

TypeDescription
stringThe base migration name fragment appended with '_not_valid'.