#36181: Composite primary key fields cannot use __in lookup with explicit 
Subquery
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                     Type:  Bug
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.2                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 In a recent PR, we
 [https://github.com/django/django/pull/19108#discussion_r1932339414
 surfaced] some inconsistencies with the resolution of explicit
 `Subquery()` such that some lookups might fail:

 {{{#!diff
 diff --git a/tests/composite_pk/test_filter.py
 b/tests/composite_pk/test_filter.py
 index 937dd86652..60d43f4a52 100644
 --- a/tests/composite_pk/test_filter.py
 +++ b/tests/composite_pk/test_filter.py
 @@ -437,6 +437,11 @@ class CompositePKFilterTests(TestCase):
          queryset = User.objects.filter(comments__in=subquery)
          self.assertSequenceEqual(queryset, (self.user_2,))

 +    def test_explicit_subquery(self):
 +        subquery = Subquery(User.objects.values("pk"))
 +        self.assertEqual(User.objects.filter(pk__in=subquery).count(), 5)
 +
 self.assertEqual(Comment.objects.filter(user__in=subquery).count(), 5)
 +
      def test_cannot_cast_pk(self):
          msg = "Cast expression does not support composite primary keys."
          with self.assertRaisesMessage(ValueError, msg):
 }}}

 gives
 {{{#!py
 ======================================================================
 ERROR: test_explicit_subquery
 (composite_pk.test_filter.CompositePKFilterTests.test_explicit_subquery)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/.../django/tests/composite_pk/test_filter.py", line 418, in
 test_explicit_subquery
     self.assertEqual(Comment.objects.filter(user__in=subquery).count(), 5)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/query.py", line 603, in count
     return self.query.get_count(using=self.db)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/query.py", line 644, in
 get_count
     return obj.get_aggregation(using, {"__count": Count("*")})["__count"]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/query.py", line 626, in
 get_aggregation
     result = compiler.execute_sql(SINGLE)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/compiler.py", line 1610, in
 execute_sql
     sql, params = self.as_sql()
                   ^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/compiler.py", line 794, in
 as_sql
     self.compile(self.where) if self.where is not None else ("", [])
     ^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/compiler.py", line 577, in
 compile
     sql, params = node.as_sql(self, self.connection)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/where.py", line 151, in
 as_sql
     sql, params = compiler.compile(child)
                   ^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/sql/compiler.py", line 577, in
 compile
     sql, params = node.as_sql(self, self.connection)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/fields/related_lookups.py",
 line 86, in as_sql
     SubqueryConstraint(
   File "/Users/.../django/django/db/models/sql/where.py", line 358, in
 __init__
     query_object.clear_ordering(clear_default=True)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 AttributeError: 'Subquery' object has no attribute 'clear_ordering'
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36181>
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/01070194ed3d8932-5e0a896e-412e-4570-8995-037cba255fbd-000000%40eu-central-1.amazonses.com.

Reply via email to