#36198: Unique Constraints defined with __date lookups on F expressions fail
validation but create database constraints sucessfully
-------------------------------------+-------------------------------------
     Reporter:  Tom Hall             |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  constraint lookup    |             Triage Stage:  Accepted
  unique validation                  |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

 * stage:  Unreviewed => Accepted

Comment:

 Thank you for the ticket!

 I can replicate and I agree that if this is not allowed it is not clear in
 the docs
 Here is a possible regression test
 {{{#!diff
 --- a/tests/constraints/models.py
 +++ b/tests/constraints/models.py
 @@ -73,6 +73,7 @@ class UniqueConstraintProduct(models.Model):
      name = models.CharField(max_length=255)
      color = models.CharField(max_length=32, null=True)
      age = models.IntegerField(null=True)
 +    updated = models.DateTimeField(null=True)

      class Meta:
          constraints = [
 diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
 index 51f09f2937..a42cf9b369 100644
 --- a/tests/constraints/tests.py
 +++ b/tests/constraints/tests.py
 @@ -1,3 +1,4 @@
 +from datetime import datetime, timedelta
  from unittest import mock

  from django.core.exceptions import ValidationError
 @@ -1030,6 +1031,21 @@ class UniqueConstraintTests(TestCase):
              exclude={"name"},
          )

 +    def test_validate_expression_date(self):
 +        updated_date = datetime(2005, 7, 26)
 +        UniqueConstraintProduct.objects.create(name="p1",
 updated=updated_date)
 +        constraint = models.UniqueConstraint(models.F("updated__date"),
 name="date_created_unique")
 +        msg = "Constraint “date_created_unique” is violated."
 +        with self.assertRaisesMessage(ValidationError, msg):
 +            constraint.validate(
 +                UniqueConstraintProduct,
 +                UniqueConstraintProduct(updated=updated_date),
 +            )
 +        constraint.validate(
 +            UniqueConstraintProduct,
 +            UniqueConstraintProduct(updated=updated_date +
 timedelta(days=1)),
 +        )
 +
      def test_validate_ordered_expression(self):
          constraint = models.UniqueConstraint(
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36198#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070195193ee158-66494aea-86e1-4b18-8be9-dba6232e1add-000000%40eu-central-1.amazonses.com.

Reply via email to