﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37314	Tuple lookup sanity checks consume iterator rhs	Jacob Walls	Django Sprints	"Similar in spirit to #37311 and #24719, we try to support iterators when provided as a lookup value by materializing them into a `list` or similar. This [https://dryorm.xterm.info/composite-primary-key-rhs-iterator fiddle] demonstrates that `CompositePrimaryKey` does not allow for this:

Notice the commented out version with `return` works fine:
{{{#!py
from django.db import models

class Tenant(models.Model):
    name = models.CharField(max_length=10, default="""", blank=True)


class Token(models.Model):
    pk = models.CompositePrimaryKey(""tenant_id"", ""id"")
    tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE, related_name=""tokens"")
    id = models.SmallIntegerField()
    secret = models.CharField(max_length=10, default="""", blank=True)


def search():
    # return 1, 1  # works
    yield 1, 1  # fails

def run():
    qs = Token.objects.filter(pk=search())
    print(qs)
}}}
{{{#!py
...
  File ""/usr/local/lib/python3.12/site-packages/django/db/models/sql/query.py"", line 1678, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 ^^^^^^^^^^^^^^^^^^
  File ""/usr/local/lib/python3.12/site-packages/django/db/models/sql/query.py"", line 1588, in build_filter
    condition = self.build_lookup(lookups, col, value)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/local/lib/python3.12/site-packages/django/db/models/sql/query.py"", line 1415, in build_lookup
    lookup = lookup_class(lhs, rhs)
             ^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/local/lib/python3.12/site-packages/django/db/models/lookups.py"", line 38, in __init__
    self.rhs = self.get_prep_lookup()
               ^^^^^^^^^^^^^^^^^^^^^^
  File ""/usr/local/lib/python3.12/site-packages/django/db/models/fields/tuple_lookups.py"", line 44, in get_prep_lookup
    self.check_rhs_length_equals_lhs_length()
  File ""/usr/local/lib/python3.12/site-packages/django/db/models/fields/tuple_lookups.py"", line 61, in check_rhs_length_equals_lhs_length
    raise ValueError(
ValueError: 'exact' lookup of 'pk' must have 2 elements
}}}

Setting to Django Sprints to allow some time for this to be assigned to someone participating in a mentorship program, but otherwise, in a few weeks we can unassign it."	Bug	assigned	Database layer (models, ORM)	5.2	Normal		iterator, CompositePrimaryKey		Unreviewed	0	0	0	0	0	0
