summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2024-07-13 22:09:50 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-17 13:07:41 +0200
commitc30669821bcda112647af4ebf5b09d3607672909 (patch)
tree00245af5e3ed63de756d74813565e8a33439c24b
parentc1028bdd0962cd2fdfdd966cd5c6243cb196d896 (diff)
[5.0.x] Refs #30581 -- Made unattached UniqueConstraint(fields) validation testable.
The logic allowing UniqueConstraint(fields).validate to preserve backward compatiblity with Model.unique_error_message failed to account for cases where the constraint might not be attached to a model which is a common pattern during testing. This changes allows for arbitrary UniqueConstraint(fields) to be tested in isolation without requiring actual models backing them up. Co-authored-by: Mark G <mark.gensler@protonmail.com> Backport of 13922580cccfb9ab2922ff4943dd39da56dfbd8c from main.
-rw-r--r--django/db/models/constraints.py19
-rw-r--r--tests/constraints/tests.py9
2 files changed, 16 insertions, 12 deletions
diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py
index 56d547e6b0..7e269b2418 100644
--- a/django/db/models/constraints.py
+++ b/django/db/models/constraints.py
@@ -439,19 +439,16 @@ class UniqueConstraint(BaseConstraint):
queryset = queryset.exclude(pk=model_class_pk)
if not self.condition:
if queryset.exists():
- if self.expressions:
+ if self.fields:
+ # When fields are defined, use the unique_error_message() for
+ # backward compatibility.
raise ValidationError(
- self.get_violation_error_message(),
- code=self.violation_error_code,
+ instance.unique_error_message(model, self.fields),
)
- # When fields are defined, use the unique_error_message() for
- # backward compatibility.
- for model, constraints in instance.get_constraints():
- for constraint in constraints:
- if constraint is self:
- raise ValidationError(
- instance.unique_error_message(model, self.fields),
- )
+ raise ValidationError(
+ self.get_violation_error_message(),
+ code=self.violation_error_code,
+ )
else:
against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
try:
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index 55df5975de..30b9472c31 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -865,6 +865,13 @@ class UniqueConstraintTests(TestCase):
ChildUniqueConstraintProduct(name=self.p1.name, color=self.p1.color),
)
+ def test_validate_fields_unattached(self):
+ Product.objects.create(price=42)
+ constraint = models.UniqueConstraint(fields=["price"], name="uniq_prices")
+ msg = "Product with this Price already exists."
+ with self.assertRaisesMessage(ValidationError, msg):
+ constraint.validate(Product, Product(price=42))
+
@skipUnlessDBFeature("supports_partial_indexes")
def test_validate_condition(self):
p1 = UniqueConstraintConditionProduct.objects.create(name="p1")
@@ -890,7 +897,7 @@ class UniqueConstraintTests(TestCase):
)
@skipUnlessDBFeature("supports_partial_indexes")
- def test_validate_conditon_custom_error(self):
+ def test_validate_condition_custom_error(self):
p1 = UniqueConstraintConditionProduct.objects.create(name="p1")
constraint = models.UniqueConstraint(
fields=["name"],