| 19 | 19 | class BaseConstraint: |
| 20 | 20 | default_violation_error_message = _("Constraint “%(name)s” is violated.") |
| 21 | 21 | violation_error_message = None |
| 22 | violation_error_code = None | |
| 22 | 23 | |
| 23 | 24 | # RemovedInDjango60Warning: When the deprecation ends, replace with: |
| 24 | # def __init__(self, *, name, violation_error_message=None): | |
| 25 | def __init__(self, *args, name=None, violation_error_message=None): | |
| 25 | # def __init__(self, *, name, violation_error_message=None, violation_error_code=None): | |
| 26 | def __init__(self, *args, name=None, violation_error_message=None, violation_error_code=None): | |
| 26 | 27 | # RemovedInDjango60Warning. |
| 27 | 28 | if name is None and not args: |
| 28 | 29 | raise TypeError( |
| 34 | 35 | self.violation_error_message = violation_error_message |
| 35 | 36 | else: |
| 36 | 37 | self.violation_error_message = self.default_violation_error_message |
| 38 | self.violation_error_code = violation_error_code | |
| 37 | 39 | # RemovedInDjango60Warning. |
| 38 | 40 | if args: |
| 39 | 41 | warnings.warn( |
| 42 | 44 | RemovedInDjango60Warning, |
| 43 | 45 | stacklevel=2, |
| 44 | 46 | ) |
| 45 | for arg, attr in zip(args, ["name", "violation_error_message"]): | |
| 47 | for arg, attr in zip(args, ["name", "violation_error_message", "violation_error_code"]): | |
| 46 | 48 | if arg: |
| 47 | 49 | setattr(self, attr, arg) |
| 48 | 50 | |
| 71 | 73 | kwargs = {"name": self.name} |
| 72 | 74 | if ( |
| 73 | 75 | self.violation_error_message is not None |
| 74 | and self.violation_error_message != self.default_violation_error_message | |
| 76 | and str(self.violation_error_message) != str(self.default_violation_error_message) | |
| 75 | 77 | ): |
| 76 | 78 | kwargs["violation_error_message"] = self.violation_error_message |
| 79 | if self.violation_error_code is not None: | |
| 80 | kwargs["violation_error_code"] = self.violation_error_code | |
| 77 | 81 | return (path, (), kwargs) |
| 78 | 82 | |
| 83 | def __eq__(self, other): | |
| 84 | if isinstance(other, BaseConstraint): | |
| 85 | return ( | |
| 86 | self.name == other.name | |
| 87 | and self.violation_error_message == other.violation_error_message | |
| 88 | and self.violation_error_code == other.violation_error_code | |
| 89 | ) | |
| 90 | return NotImplemented | |
| 91 | ||
| 79 | 92 | def clone(self): |
| 80 | 93 | _, args, kwargs = self.deconstruct() |
| 81 | 94 | return self.__class__(*args, **kwargs) |
| 82 | 95 | |
| 83 | 96 | |
| 84 | 97 | class CheckConstraint(BaseConstraint): |
| 85 | def __init__(self, *, check, name, violation_error_message=None): | |
| 98 | def __init__(self, *, check, name, violation_error_message=None, violation_error_code=None): | |
| 86 | 99 | self.check = check |
| 87 | 100 | if not getattr(check, "conditional", False): |
| 88 | 101 | raise TypeError( |
| 89 | 102 | "CheckConstraint.check must be a Q instance or boolean expression." |
| 90 | 103 | ) |
| 91 | super().__init__(name=name, violation_error_message=violation_error_message) | |
| 104 | super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code) | |
| 92 | 105 | |
| 93 | 106 | def _get_check_sql(self, model, schema_editor): |
| 94 | 107 | query = Query(model=model, alias_cols=False) |
| 112 | 125 | against = instance._get_field_value_map(meta=model._meta, exclude=exclude) |
| 113 | 126 | try: |
| 114 | 127 | if not Q(self.check).check(against, using=using): |
| 115 | raise ValidationError(self.get_violation_error_message()) | |
| 128 | raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code) | |
| 116 | 129 | except FieldError: |
| 117 | 130 | pass |
| 118 | 131 | |
| 124 | 137 | ( |
| 125 | 138 | "" |
| 126 | 139 | if self.violation_error_message is None |
| 127 | or self.violation_error_message == self.default_violation_error_message | |
| 140 | or str(self.violation_error_message) == str(self.default_violation_error_message) | |
| 128 | 141 | else " violation_error_message=%r" % self.violation_error_message |
| 129 | 142 | ), |
| 130 | 143 | ) |
| 135 | 148 | self.name == other.name |
| 136 | 149 | and self.check == other.check |
| 137 | 150 | and self.violation_error_message == other.violation_error_message |
| 151 | and self.violation_error_code == other.violation_error_code | |
| 138 | 152 | ) |
| 139 | 153 | return super().__eq__(other) |
| 140 | 154 | |
| 164 | 178 | include=None, |
| 165 | 179 | opclasses=(), |
| 166 | 180 | violation_error_message=None, |
| 181 | violation_error_code=None, | |
| 167 | 182 | ): |
| 168 | 183 | if not name: |
| 169 | 184 | raise ValueError("A unique constraint must be named.") |
| 213 | 228 | F(expression) if isinstance(expression, str) else expression |
| 214 | 229 | for expression in expressions |
| 215 | 230 | ) |
| 216 | super().__init__(name=name, violation_error_message=violation_error_message) | |
| 231 | super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code) | |
| 217 | 232 | |
| 218 | 233 | @property |
| 219 | 234 | def contains_expressions(self): |
| 305 | 320 | ( |
| 306 | 321 | "" |
| 307 | 322 | if self.violation_error_message is None |
| 308 | or self.violation_error_message == self.default_violation_error_message | |
| 323 | or str(self.violation_error_message) == str(self.default_violation_error_message) | |
| 309 | 324 | else " violation_error_message=%r" % self.violation_error_message |
| 310 | 325 | ), |
| 311 | 326 | ) |
| 321 | 336 | and self.opclasses == other.opclasses |
| 322 | 337 | and self.expressions == other.expressions |
| 323 | 338 | and self.violation_error_message == other.violation_error_message |
| 339 | and self.violation_error_code == other.violation_error_code | |
| 324 | 340 | ) |
| 325 | 341 | return super().__eq__(other) |
| 326 | 342 | |
| 385 | 401 | if not self.condition: |
| 386 | 402 | if queryset.exists(): |
| 387 | 403 | if self.expressions: |
| 388 | raise ValidationError(self.get_violation_error_message()) | |
| 404 | raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code) | |
| 389 | 405 | # When fields are defined, use the unique_error_message() for |
| 390 | 406 | # backward compatibility. |
| 391 | 407 | for model, constraints in instance.get_constraints(): |
| 392 | 408 | for constraint in constraints: |
| 393 | 409 | if constraint is self: |
| 394 | raise ValidationError( | |
| 395 | instance.unique_error_message(model, self.fields) | |
| 396 | ) | |
| 410 | error_dict = instance.unique_error_message(model, self.fields) | |
| 411 | if self.violation_error_code is not None: | |
| 412 | # Add the code to all errors in the dict | |
| 413 | for field, errors in error_dict.items(): | |
| 414 | for error in errors: | |
| 415 | if hasattr(error, "code") and error.code is None: | |
| 416 | error.code = self.violation_error_code | |
| 417 | raise ValidationError(error_dict) | |
| 397 | 418 | else: |
| 398 | 419 | against = instance._get_field_value_map(meta=model._meta, exclude=exclude) |
| 399 | 420 | try: |
| 400 | 421 | if (self.condition & Exists(queryset.filter(self.condition))).check( |
| 401 | 422 | against, using=using |
| 402 | 423 | ): |
| 403 | raise ValidationError(self.get_violation_error_message()) | |
| 424 | raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code) | |
| 404 | 425 | except FieldError: |
| 405 | 426 | pass |
| Test Name | Status |
|---|---|
test_repr_with_violation_error_code (constraints.tests.CheckConstraintTests.test_repr_with_violation_error_code) | Fail |
test_repr_with_violation_error_code (constraints.tests.UniqueConstraintTests.test_repr_with_violation_error_code) | Fail |
test_validate (constraints.tests.UniqueConstraintTests.test_validate) | Fail |
test_custom_violation_code_message (constraints.tests.BaseConstraintTests.test_custom_violation_code_message) | Pass |
test_deconstruction (constraints.tests.BaseConstraintTests.test_deconstruction) | Pass |
test_eq (constraints.tests.CheckConstraintTests.test_eq) | Pass |
test_validate_custom_error (constraints.tests.CheckConstraintTests.test_validate_custom_error) | Pass |
test_eq (constraints.tests.UniqueConstraintTests.test_eq) | Pass |
test_validate_conditon_custom_error (constraints.tests.UniqueConstraintTests.test_validate_conditon_custom_error) | Pass |
test_constraint_sql (constraints.tests.BaseConstraintTests.test_constraint_sql) | Pass |
test_contains_expressions (constraints.tests.BaseConstraintTests.test_contains_expressions) | Pass |
test_create_sql (constraints.tests.BaseConstraintTests.test_create_sql) | Pass |
test_custom_violation_error_message (constraints.tests.BaseConstraintTests.test_custom_violation_error_message) | Pass |
test_custom_violation_error_message_clone (constraints.tests.BaseConstraintTests.test_custom_violation_error_message_clone) | Pass |
test_default_violation_error_message (constraints.tests.BaseConstraintTests.test_default_violation_error_message) | Pass |
test_deprecation (constraints.tests.BaseConstraintTests.test_deprecation) | Pass |
test_name_required (constraints.tests.BaseConstraintTests.test_name_required) | Pass |
test_positional_arguments (constraints.tests.BaseConstraintTests.test_positional_arguments) | Pass |
test_remove_sql (constraints.tests.BaseConstraintTests.test_remove_sql) | Pass |
test_validate (constraints.tests.BaseConstraintTests.test_validate) | Pass |
test_abstract_name (constraints.tests.CheckConstraintTests.test_abstract_name) | Pass |
test_database_constraint (constraints.tests.CheckConstraintTests.test_database_constraint) | Pass |
test_database_constraint_unicode (constraints.tests.CheckConstraintTests.test_database_constraint_unicode) | Pass |
test_deconstruction (constraints.tests.CheckConstraintTests.test_deconstruction) | Pass |
test_invalid_check_types (constraints.tests.CheckConstraintTests.test_invalid_check_types) | Pass |
test_name (constraints.tests.CheckConstraintTests.test_name) | Pass |
test_repr (constraints.tests.CheckConstraintTests.test_repr) | Pass |
test_repr_with_violation_error_message (constraints.tests.CheckConstraintTests.test_repr_with_violation_error_message) | Pass |
test_validate (constraints.tests.CheckConstraintTests.test_validate) | Pass |
test_validate_boolean_expressions (constraints.tests.CheckConstraintTests.test_validate_boolean_expressions) | Pass |
test_validate_nullable_field_with_none (constraints.tests.CheckConstraintTests.test_validate_nullable_field_with_none) | Pass |
test_validate_rawsql_expressions_noop (constraints.tests.CheckConstraintTests.test_validate_rawsql_expressions_noop) | Pass |
test_condition_must_be_q (constraints.tests.UniqueConstraintTests.test_condition_must_be_q) | Pass |
test_database_constraint (constraints.tests.UniqueConstraintTests.test_database_constraint) | Pass |
test_database_constraint_with_condition (constraints.tests.UniqueConstraintTests.test_database_constraint_with_condition) | Pass |
test_deconstruction (constraints.tests.UniqueConstraintTests.test_deconstruction) | Pass |
test_deconstruction_with_condition (constraints.tests.UniqueConstraintTests.test_deconstruction_with_condition) | Pass |
test_deconstruction_with_deferrable (constraints.tests.UniqueConstraintTests.test_deconstruction_with_deferrable) | Pass |
test_deconstruction_with_expressions (constraints.tests.UniqueConstraintTests.test_deconstruction_with_expressions) | Pass |
test_deconstruction_with_include (constraints.tests.UniqueConstraintTests.test_deconstruction_with_include) | Pass |
test_deconstruction_with_opclasses (constraints.tests.UniqueConstraintTests.test_deconstruction_with_opclasses) | Pass |
test_deferrable_with_condition (constraints.tests.UniqueConstraintTests.test_deferrable_with_condition) | Pass |
test_deferrable_with_expressions (constraints.tests.UniqueConstraintTests.test_deferrable_with_expressions) | Pass |
test_deferrable_with_include (constraints.tests.UniqueConstraintTests.test_deferrable_with_include) | Pass |
test_deferrable_with_opclasses (constraints.tests.UniqueConstraintTests.test_deferrable_with_opclasses) | Pass |
test_eq_with_condition (constraints.tests.UniqueConstraintTests.test_eq_with_condition) | Pass |
test_eq_with_deferrable (constraints.tests.UniqueConstraintTests.test_eq_with_deferrable) | Pass |
test_eq_with_expressions (constraints.tests.UniqueConstraintTests.test_eq_with_expressions) | Pass |
test_eq_with_include (constraints.tests.UniqueConstraintTests.test_eq_with_include) | Pass |
test_eq_with_opclasses (constraints.tests.UniqueConstraintTests.test_eq_with_opclasses) | Pass |
test_expressions_and_fields_mutually_exclusive (constraints.tests.UniqueConstraintTests.test_expressions_and_fields_mutually_exclusive) | Pass |
test_expressions_with_opclasses (constraints.tests.UniqueConstraintTests.test_expressions_with_opclasses) | Pass |
test_invalid_defer_argument (constraints.tests.UniqueConstraintTests.test_invalid_defer_argument) | Pass |
test_invalid_include_argument (constraints.tests.UniqueConstraintTests.test_invalid_include_argument) | Pass |
test_invalid_opclasses_argument (constraints.tests.UniqueConstraintTests.test_invalid_opclasses_argument) | Pass |
test_model_validation (constraints.tests.UniqueConstraintTests.test_model_validation) | Pass |
test_model_validation_constraint_no_code_error (constraints.tests.UniqueConstraintTests.test_model_validation_constraint_no_code_error) | Pass |
Partial unique constraints are not ignored by | Pass |
test_name (constraints.tests.UniqueConstraintTests.test_name) | Pass |
test_opclasses_and_fields_same_length (constraints.tests.UniqueConstraintTests.test_opclasses_and_fields_same_length) | Pass |
test_repr (constraints.tests.UniqueConstraintTests.test_repr) | Pass |
test_repr_with_condition (constraints.tests.UniqueConstraintTests.test_repr_with_condition) | Pass |
test_repr_with_deferrable (constraints.tests.UniqueConstraintTests.test_repr_with_deferrable) | Pass |
test_repr_with_expressions (constraints.tests.UniqueConstraintTests.test_repr_with_expressions) | Pass |
test_repr_with_include (constraints.tests.UniqueConstraintTests.test_repr_with_include) | Pass |
test_repr_with_opclasses (constraints.tests.UniqueConstraintTests.test_repr_with_opclasses) | Pass |
test_repr_with_violation_error_message (constraints.tests.UniqueConstraintTests.test_repr_with_violation_error_message) | Pass |
test_requires_field_or_expression (constraints.tests.UniqueConstraintTests.test_requires_field_or_expression) | Pass |
test_requires_name (constraints.tests.UniqueConstraintTests.test_requires_name) | Pass |
test_validate_condition (constraints.tests.UniqueConstraintTests.test_validate_condition) | Pass |
test_validate_expression (constraints.tests.UniqueConstraintTests.test_validate_expression) | Pass |
test_validate_expression_condition (constraints.tests.UniqueConstraintTests.test_validate_expression_condition) | Pass |
test_validate_expression_str (constraints.tests.UniqueConstraintTests.test_validate_expression_str) | Pass |
test_validate_ordered_expression (constraints.tests.UniqueConstraintTests.test_validate_ordered_expression) | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.