Opened 15 hours ago

Last modified 3 hours ago

#36042 new Bug

Lookups fail when rhs wraps CompositePrimaryKey in F and lhs is another field

Reported by: Jacob Walls Owned by:
Component: Database layer (models, ORM) Version: dev
Severity: Release blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Failing tests:

  • tests/composite_pk/test_filter.py

    diff --git a/tests/composite_pk/test_filter.py b/tests/composite_pk/test_filter.py
    index 7e361c5925..beb4d44325 100644
    a b  
     1from django.db.models import F, TextField
     2from django.db.models.functions import Cast
    13from django.test import TestCase
    24
    35from .models import Comment, Tenant, User
    class CompositePKFilterTests(TestCase):  
    5456            with self.subTest(lookup=lookup, count=count):
    5557                self.assertEqual(User.objects.filter(**lookup).count(), count)
    5658
     59    def test_f_pk(self):
     60        with self.assertRaises(ValueError):
     61            Comment.objects.filter(text__gt=F("pk")).count()
     62
     63    def test_cast_pk(self):
     64        self.assertSequenceEqual(
     65            Comment.objects.filter(text__gt=Cast(F("pk"), TextField())),
     66            []  # or whatever the appropriate result is
     67        )
     68
    5769    def test_order_comments_by_pk_asc(self):
    5870        self.assertSequenceEqual(
    5971            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)

Change History (2)

comment:1 by Jacob Walls, 14 hours ago

Type: UncategorizedBug

comment:2 by Jacob Walls, 3 hours ago

Summary: Lookups fail when rhs wraps CompositeForeignKey in F and lhs is another fieldLookups fail when rhs wraps CompositePrimaryKey in F and lhs is another field
Note: See TracTickets for help on using tickets.
Back to Top