let it go

Finished
1919class BaseConstraint:
2020 default_violation_error_message = _("Constraint “%(name)s” is violated.")
2121 violation_error_message = None
22 violation_error_code = None
2223
2324 # 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):
2627 # RemovedInDjango60Warning.
2728 if name is None and not args:
2829 raise TypeError(
3435 self.violation_error_message = violation_error_message
3536 else:
3637 self.violation_error_message = self.default_violation_error_message
38 self.violation_error_code = violation_error_code
3739 # RemovedInDjango60Warning.
3840 if args:
3941 warnings.warn(
4244 RemovedInDjango60Warning,
4345 stacklevel=2,
4446 )
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"]):
4648 if arg:
4749 setattr(self, attr, arg)
4850
7476 and self.violation_error_message != self.default_violation_error_message
7577 ):
7678 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
7781 return (path, (), kwargs)
7882
7983 def clone(self):
8286
8387
8488class CheckConstraint(BaseConstraint):
85 def __init__(self, *, check, name, violation_error_message=None):
89 def __init__(self, *, check, name, violation_error_message=None, violation_error_code=None):
8690 self.check = check
8791 if not getattr(check, "conditional", False):
8892 raise TypeError(
8993 "CheckConstraint.check must be a Q instance or boolean expression."
9094 )
91 super().__init__(name=name, violation_error_message=violation_error_message)
95 super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code)
9296
9397 def _get_check_sql(self, model, schema_editor):
9498 query = Query(model=model, alias_cols=False)
112116 against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
113117 try:
114118 if not Q(self.check).check(against, using=using):
115 raise ValidationError(self.get_violation_error_message())
119 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
116120 except FieldError:
117121 pass
118122
123127 repr(self.name),
124128 (
125129 ""
126 if self.violation_error_message is None
127 or self.violation_error_message == self.default_violation_error_message
130 if (self.violation_error_message is None
131 or self.violation_error_message == self.default_violation_error_message)
132 and self.violation_error_code is None
128133 else " violation_error_message=%r" % self.violation_error_message
134 + ("" if self.violation_error_code is None else f" violation_error_code={self.violation_error_code!r}")
129135 ),
130136 )
131137
135141 self.name == other.name
136142 and self.check == other.check
137143 and self.violation_error_message == other.violation_error_message
144 and self.violation_error_code == other.violation_error_code
138145 )
139146 return super().__eq__(other)
140147
164171 include=None,
165172 opclasses=(),
166173 violation_error_message=None,
174 violation_error_code=None,
167175 ):
168176 if not name:
169177 raise ValueError("A unique constraint must be named.")
213221 F(expression) if isinstance(expression, str) else expression
214222 for expression in expressions
215223 )
216 super().__init__(name=name, violation_error_message=violation_error_message)
224 super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code)
217225
218226 @property
219227 def contains_expressions(self):
304312 "" if not self.opclasses else " opclasses=%s" % repr(self.opclasses),
305313 (
306314 ""
307 if self.violation_error_message is None
308 or self.violation_error_message == self.default_violation_error_message
315 if (self.violation_error_message is None
316 or self.violation_error_message == self.default_violation_error_message)
317 and self.violation_error_code is None
309318 else " violation_error_message=%r" % self.violation_error_message
319 + ("" if self.violation_error_code is None else f" violation_error_code={self.violation_error_code!r}")
310320 ),
311321 )
312322
321331 and self.opclasses == other.opclasses
322332 and self.expressions == other.expressions
323333 and self.violation_error_message == other.violation_error_message
334 and self.violation_error_code == other.violation_error_code
324335 )
325336 return super().__eq__(other)
326337
385396 if not self.condition:
386397 if queryset.exists():
387398 if self.expressions:
388 raise ValidationError(self.get_violation_error_message())
399 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
389400 # When fields are defined, use the unique_error_message() for
390401 # backward compatibility.
391402 for model, constraints in instance.get_constraints():
392403 for constraint in constraints:
393404 if constraint is self:
394405 raise ValidationError(
395 instance.unique_error_message(model, self.fields)
406 instance.unique_error_message(model, self.fields), code=self.violation_error_code
396407 )
397408 else:
398409 against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
400411 if (self.condition & Exists(queryset.filter(self.condition))).check(
401412 against, using=using
402413 ):
403 raise ValidationError(self.get_violation_error_message())
414 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
404415 except FieldError:
405416 pass
Test NameStatus
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_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 (constraints.tests.UniqueConstraintTests.test_validate)
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.