Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#36392 closed Bug (fixed)

ValueError not raised when wrong number of columns selected by subquery against composite primary key

Reported by: Jacob Walls Owned by: Jacob Walls
Component: Database layer (models, ORM) Version: 5.2
Severity: Release blocker Keywords: pk
Cc: 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

Discovered during review of #36210 that the guard for the appropriate number of columns selected in a subquery is not aware that "pk" might represent N columns, so the ValueError that should be raised is evaded, failing instead at the db layer.

We're just changing one type of exception to another--so this isn't terribly serious--but in the interest of standardizing what kinds of exceptions are raised during usage of the composite pk feature, starting out at release blocker?

  • tests/composite_pk/test_filter.py

    diff --git a/tests/composite_pk/test_filter.py b/tests/composite_pk/test_filter.py
    index 16c4c2a91a..f629f85ca6 100644
    a b class CompositePKFilterTests(TestCase):  
    207207            [self.comment_1],
    208208        )
    209209
     210    def test_invalid_filter_by_pk_in_columns_besides_pk(self):
     211        with self.assertRaisesMessage(ValueError, "must have 2 selected fields"):
     212            User.objects.filter(pk__in=User.objects.values("pk", "email")).count()
     213
    210214    def test_filter_by_pk_in_none(self):
    211215        with self.assertNumQueries(0):
    212216            self.assertSequenceEqual(

Postgres failure looks like:

  File "/Users/.../django/django/db/backends/sqlite3/base.py", line 360, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: sub-select returns 3 columns - expected 2

Change History (8)

comment:1 by Clifford Gama, 2 months ago

Triage Stage: UnreviewedAccepted

comment:2 by Jacob Walls, 2 months ago

Owner: set to Jacob Walls
Status: newassigned

comment:3 by Jacob Walls, 2 months ago

Has patch: set

comment:4 by Sarah Boyce, 2 months ago

Patch needs improvement: set

comment:5 by Jacob Walls, 2 months ago

Patch needs improvement: unset

comment:6 by Sarah Boyce, 2 months ago

Triage Stage: AcceptedReady for checkin

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 2 months ago

Resolution: fixed
Status: assignedclosed

In 994dc6d8:

Fixed #36392 -- Raised ValueError when subquery referencing composite pk selects too many columns.

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 2 months ago

In 6228a35:

[5.2.x] Fixed #36392 -- Raised ValueError when subquery referencing composite pk selects too many columns.

Backport of 994dc6d8a1bae717baa236b65e11cf91ce181c53 from main.

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