Opened 6 weeks ago

Closed 4 weeks ago

Last modified 2 weeks ago

#36042 closed Bug (fixed)

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

Reported by: Jacob Walls Owned by: Jacob Walls
Component: Database layer (models, ORM) Version: dev
Severity: Release blocker Keywords:
Cc: Mariusz Felisiak Triage Stage: Ready for checkin
Has patch: yes 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 (13)

comment:1 by Jacob Walls, 6 weeks ago

Type: UncategorizedBug

comment:2 by Jacob Walls, 6 weeks 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

comment:3 by Tim Graham, 5 weeks ago

Triage Stage: UnreviewedAccepted

comment:4 by Mariusz Felisiak, 5 weeks ago

Cc: Mariusz Felisiak added

Jacob, would you like to prepare a patch?

comment:5 by Jacob Walls, 5 weeks ago

Owner: set to Jacob Walls
Status: newassigned

I'll give it a shot.

comment:6 by Jacob Walls, 5 weeks ago

Has patch: set

comment:7 by Jacob Walls, 5 weeks ago

Patch needs improvement: set

Will update along the lines of comment:3:ticket:36051.

comment:8 by Jacob Walls, 4 weeks ago

Patch needs improvement: unset

comment:9 by Sarah Boyce, 4 weeks ago

Triage Stage: AcceptedReady for checkin

comment:10 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In 6eec703:

Fixed #36042 -- Raised ValueError when using CompositePrimaryKey as rhs.

comment:11 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

In 470e554:

Refs #36042 -- Raised ValueError when providing composite expressions to aggregates.

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 2 weeks ago

In a76035e:

Refs #36042 -- Consolidated composite expression checks in BaseExpression.

Remove redundant Func.resolve_expression and adjust CombinedExpression to
delegate source expression resolving to super() to perform checks against
allows_composite_expressions in a single location.

comment:13 by Sarah Boyce <42296566+sarahboyce@…>, 2 weeks ago

In e306687a:

[5.2.x] Refs #36042 -- Consolidated composite expression checks in BaseExpression.

Remove redundant Func.resolve_expression and adjust CombinedExpression to
delegate source expression resolving to super() to perform checks against
allows_composite_expressions in a single location.

Backport of a76035e925ff4e6d8676c65cb135c74b993b1039 from main.

Note: See TracTickets for help on using tickets.
Back to Top