Opened 67 minutes ago

Last modified 3 minutes ago

#37273 assigned Bug

Composite primary key exact lookups don’t support F() expressions on the RHS

Reported by: Pravin Owned by: Pravin
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: CompositeField, CompositePrimaryKey, Filter, RHS
Cc: Pravin Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

An exact lookup on a CompositePrimaryKey accepts a literal tuple but rejects an F() expression referencing a compatible composite primary key.

for example,

class Comment(models.Model):
    pk = models.CompositePrimaryKey("tenant_id", "id")
    tenant_id = models.IntegerField()
    id = models.IntegerField()
    text = models.TextField()

The following works.

comment = Comment.objects.first()
Comment.objects.filter(pk=(comment.tenant_id, comment.id))

but below one fails.

Comment.objects.filter(pk=models.F("pk"))

it raises
ValueError: 'exact' subquery lookup of 'pk' only supports OuterRef and QuerySet objects (received 'ColPairs')

Change History (4)

comment:1 by Sarah Boyce, 47 minutes ago

Triage Stage: UnreviewedAccepted

Thank you for the report! Replicated
This is potentially a quick fix but I will let you investigate further

  • django/db/models/fields/tuple_lookups.py

    diff --git a/django/db/models/fields/tuple_lookups.py b/django/db/models/fields/tuple_lookups.py
    index 69db531802..19f05963bc 100644
    a b class TupleLookupMixin:  
    7474            )
    7575
    7676    def check_rhs_is_supported_expression(self):
    77         if not isinstance(self.rhs, (ResolvedOuterRef, Query)):
     77        if not isinstance(self.rhs, (ColPairs, ResolvedOuterRef, Query)):
    7878            lhs_str = self.get_lhs_str()
    7979            rhs_cls = self.rhs.__class__.__name__
    8080            raise ValueError(
  • tests/composite_pk/test_filter.py

    diff --git a/tests/composite_pk/test_filter.py b/tests/composite_pk/test_filter.py
    index fdaa323fd8..376dd61b3d 100644
    a b class CompositePKFilterTests(TestCase):  
    7575        with self.assertRaisesMessage(ValueError, msg):
    7676            Comment.objects.filter(text__gt=F("pk")).count()
    7777
     78    def test_exact_f_expression_rhs(self):
     79        self.assertQuerySetEqual(
     80            Comment.objects.filter(pk=F("pk")),
     81            Comment.objects.all(),
     82            ordered=False,
     83        )
     84
    7885    def test_rhs_combinable(self):
    7986        msg = "CombinedExpression expression does not support composite primary keys."

comment:2 by Pravin, 32 minutes ago

Thanks Sarah. Do we need to update release doc for it ? if yes then which version ?

Last edited 28 minutes ago by Pravin (previous) (diff)

comment:3 by Sarah Boyce, 14 minutes ago

No release note needed, we won't backport this fix

comment:4 by Pravin, 3 minutes ago

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