#36042: Lookups fail when rhs wraps CompositeForeignKey in F and lhs is another
field
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                     Type:
                                     |  Uncategorized
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  dev                  |                 Severity:  Release
                                     |  blocker
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 Failing tests:

 {{{#!diff
 diff --git a/tests/composite_pk/test_filter.py
 b/tests/composite_pk/test_filter.py
 index 7e361c5925..beb4d44325 100644
 --- a/tests/composite_pk/test_filter.py
 +++ b/tests/composite_pk/test_filter.py
 @@ -1,3 +1,5 @@
 +from django.db.models import F, TextField
 +from django.db.models.functions import Cast
  from django.test import TestCase

  from .models import Comment, Tenant, User
 @@ -54,6 +56,16 @@ class CompositePKFilterTests(TestCase):
              with self.subTest(lookup=lookup, count=count):
                  self.assertEqual(User.objects.filter(**lookup).count(),
 count)

 +    def test_f_pk(self):
 +        with self.assertRaises(ValueError):
 +            Comment.objects.filter(text__gt=F("pk")).count()
 +
 +    def test_cast_pk(self):
 +        self.assertSequenceEqual(
 +            Comment.objects.filter(text__gt=Cast(F("pk"), TextField())),
 +            []  # or whatever the appropriate result is
 +        )
 +
      def test_order_comments_by_pk_asc(self):
          self.assertSequenceEqual(
              Comment.objects.order_by("pk"),
 }}}

 Failures:
 {{{
 ======================================================================
 ERROR: test_cast_pk
 (composite_pk.test_filter.CompositePKFilterTests.test_cast_pk)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/.../django/django/db/backends/utils.py", line 105, in
 _execute
     return self.cursor.execute(sql, params)
            ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
   File "/Users/.../django/django/db/backends/sqlite3/base.py", line 360,
 in execute
     return super().execute(query, params)
            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
 sqlite3.OperationalError: near ",": syntax error

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "/Users/.../django/tests/composite_pk/test_filter.py", line 64, in
 test_cast_pk
     self.assertSequenceEqual(
     ~~~~~~~~~~~~~~~~~~~~~~~~^
         Comment.objects.filter(text__gt=Cast(F("pk"), TextField())),
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         []  # or whatever the appropriate result is
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     )
     ^
   File
 
"/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/unittest/case.py",
 line 1025, in assertSequenceEqual
     len1 = len(seq1)
   File "/Users/.../django/django/db/models/query.py", line 366, in __len__
     self._fetch_all()
     ~~~~~~~~~~~~~~~^^
   File "/Users/.../django/django/db/models/query.py", line 1930, in
 _fetch_all
     self._result_cache = list(self._iterable_class(self))
                          ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/models/query.py", line 91, in __iter__
     results = compiler.execute_sql(
         chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size
     )
   File "/Users/.../django/django/db/models/sql/compiler.py", line 1624, in
 execute_sql
     cursor.execute(sql, params)
     ~~~~~~~~~~~~~~^^^^^^^^^^^^^
   File "/Users/.../django/django/db/backends/utils.py", line 79, in
 execute
     return self._execute_with_wrappers(
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
         sql, params, many=False, executor=self._execute
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     )
     ^
   File "/Users/.../django/django/db/backends/utils.py", line 92, in
 _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/Users/.../django/django/db/backends/utils.py", line 100, in
 _execute
     with self.db.wrap_database_errors:
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/utils.py", line 91, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/Users/.../django/django/db/backends/utils.py", line 105, in
 _execute
     return self.cursor.execute(sql, params)
            ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
   File "/Users/.../django/django/db/backends/sqlite3/base.py", line 360,
 in execute
     return super().execute(query, params)
            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
 django.db.utils.OperationalError: near ",": syntax error

 ======================================================================
 ERROR: test_f_pk
 (composite_pk.test_filter.CompositePKFilterTests.test_f_pk)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/.../django/django/db/backends/utils.py", line 105, in
 _execute
     return self.cursor.execute(sql, params)
            ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
   File "/Users/.../django/django/db/backends/sqlite3/base.py", line 360,
 in execute
     return super().execute(query, params)
            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
 sqlite3.OperationalError: row value misused

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "/Users/.../django/tests/composite_pk/test_filter.py", line 61, in
 test_f_pk
     Comment.objects.filter(text__gt=F("pk")).count()
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
   File "/Users/.../django/django/db/models/query.py", line 604, 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 1624, in
 execute_sql
     cursor.execute(sql, params)
     ~~~~~~~~~~~~~~^^^^^^^^^^^^^
   File "/Users/.../django/django/db/backends/utils.py", line 79, in
 execute
     return self._execute_with_wrappers(
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
         sql, params, many=False, executor=self._execute
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     )
     ^
   File "/Users/.../django/django/db/backends/utils.py", line 92, in
 _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/Users/.../django/django/db/backends/utils.py", line 100, in
 _execute
     with self.db.wrap_database_errors:
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/.../django/django/db/utils.py", line 91, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/Users/.../django/django/db/backends/utils.py", line 105, in
 _execute
     return self.cursor.execute(sql, params)
            ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
   File "/Users/.../django/django/db/backends/sqlite3/base.py", line 360,
 in execute
     return super().execute(query, params)
            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
 django.db.utils.OperationalError: row value misused

 ----------------------------------------------------------------------
 Ran 103 tests in 0.081s

 FAILED (errors=2)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36042>
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/010701940e8df9ac-a77215b3-7c86-4d8f-ba6d-524c76628a5e-000000%40eu-central-1.amazonses.com.

Reply via email to