#35329: Bug UniqueConstraint with condition and nulls-distinct
-------------------------------------+-------------------------------------
     Reporter:  Lucas Lemke          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:  nulls-distinct,      |             Triage Stage:  Accepted
  condition, UniqueConstraint        |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

 Replying to [comment:1 Simon Charette]:
 > Adding a test should be as simple as taking inspiration from the ones
 [https://github.com/django/django/pull/17058/files#diff-
 40b7bac727110526783c1fd39a2b6026c9e01862b7e50b21fa89e9a3591bd2d7R3321-R3343
 introduced when the feature was added].

 A regression test:
 {{{#!diff
 diff --git a/tests/schema/tests.py b/tests/schema/tests.py
 index b912d353eb..f8e314d270 100644
 --- a/tests/schema/tests.py
 +++ b/tests/schema/tests.py
 @@ -3629,6 +3629,38 @@ class SchemaTests(TransactionTestCase):
          constraints = self.get_constraints(Author._meta.db_table)
          self.assertNotIn(constraint.name, constraints)

 +    @skipUnlessDBFeature(
 +        "supports_nulls_distinct_unique_constraints",
 +        "supports_partial_indexes",
 +    )
 +    def test_unique_constraint_nulls_distinct_condition(self):
 +        with connection.schema_editor() as editor:
 +            editor.create_model(Author)
 +        constraint = UniqueConstraint(
 +            fields=["height", "weight"],
 +            name="un_height_weight_start_A",
 +            condition=Q(name__startswith="A"),
 +            nulls_distinct=False,
 +        )
 +        with connection.schema_editor() as editor:
 +            editor.add_constraint(Author, constraint)
 +        Author.objects.create(name="Adam", height=None, weight=None)
 +        Author.objects.create(name="Avocado", height=1, weight=None)
 +        Author.objects.create(name="Adrian", height=None, weight=1)
 +        with self.assertRaises(IntegrityError):
 +            Author.objects.create(name="Alex", height=None, weight=None)
 +        Author.objects.create(name="Bob", height=None, weight=None)
 +        with self.assertRaises(IntegrityError):
 +            Author.objects.create(name="Alex", height=1, weight=None)
 +        Author.objects.create(name="Bill", height=None, weight=None)
 +        with self.assertRaises(IntegrityError):
 +            Author.objects.create(name="Alex", height=None, weight=1)
 +        Author.objects.create(name="Celine", height=None, weight=1)
 +        with connection.schema_editor() as editor:
 +            editor.remove_constraint(Author, constraint)
 +        constraints = self.get_constraints(Author._meta.db_table)
 +        self.assertNotIn(constraint.name, constraints)
 +
      @skipIfDBFeature("supports_nulls_distinct_unique_constraints")
      def test_unique_constraint_nulls_distinct_unsupported(self):
          # UniqueConstraint is ignored on databases that don't support

 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35329#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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e79f0cd99-e3f7dee7-71b1-4f85-86b7-394190c613e1-000000%40eu-central-1.amazonses.com.

Reply via email to