1
0
mirror of https://github.com/django/django.git synced 2025-10-25 06:36:07 +00:00

Fixed #34871, #36518 -- Implemented unresolved lookups expression replacement.

This allows the proper resolving of lookups when performing constraint
validation involving Q and Case objects.

Thanks Andrew Roberts for the report and Sarah for the tests and review.
This commit is contained in:
Simon Charette
2025-02-19 03:04:16 -05:00
committed by Sarah Boyce
parent 500bd42b96
commit 079d31e698
4 changed files with 124 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ from unittest import mock
from django.core.exceptions import ValidationError
from django.db import IntegrityError, connection, models
from django.db.models import F
from django.db.models import Case, F, When
from django.db.models.constraints import BaseConstraint, UniqueConstraint
from django.db.models.functions import Abs, Lower, Sqrt, Upper
from django.db.transaction import atomic
@@ -1064,6 +1064,23 @@ class UniqueConstraintTests(TestCase):
UniqueConstraintProduct(updated=updated_date + timedelta(days=1)),
)
def test_validate_case_when(self):
UniqueConstraintProduct.objects.create(name="p1")
constraint = models.UniqueConstraint(
Case(When(color__isnull=True, then=F("name"))),
name="name_without_color_uniq",
)
msg = "Constraint “name_without_color_uniq” is violated."
with self.assertRaisesMessage(ValidationError, msg):
constraint.validate(
UniqueConstraintProduct,
UniqueConstraintProduct(name="p1"),
)
constraint.validate(
UniqueConstraintProduct,
UniqueConstraintProduct(name="p1", color="green"),
)
def test_validate_ordered_expression(self):
constraint = models.UniqueConstraint(
Lower("name").desc(), name="name_lower_uniq_desc"