8000 Fixed #36210 -- Allowed Subquery usage in further lookups against composite pks. by jacobtylerwalls · Pull Request #19459 · django/django · GitHub
[go: up one dir, main page]

Skip to content

Fixed #36210 -- Allowed Subquery usage in further lookups against composite pks. #19459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jacobtylerwalls
Copy link
Member
@jacobtylerwalls jacobtylerwalls commented May 12, 2025

Trac ticket number

ticket-36210

Branch description

Following-up to ticket-36181 (#19160), which added support for pk__in=Subquery(... for composite primary keys, now other lookups besides in are supported, e.g. exact.

Because lookups besides in require limiting subqueries to a single result, handling this correctly uncovered situations where cardinality violations were flying under the radar, so a preparatory commit fixes correctness in some tests.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • [n/a] I have added or updated relevant docs, including release notes if applicable.
  • [n/a] I have attached screenshots in both light and dark modes for any UI changes.

8000
@@ -1756,6 +1756,7 @@ def __init__(self, queryset, output_field=None, **extra):
# Allow the usage of both QuerySet and sql.Query objects.
self.query = getattr(queryset, "query", queryset).clone()
self.query.subquery = True
self.template = extra.pop("template", self.template)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately avoided adding tests for extra, given that discussion in ticket-35459 is ongoing. There is of course implicit coverage that a self.template attribute exists.

@jacobtylerwalls

This comment was marked as outdated.

@jacobtylerwalls

This comment was marked as outdated.

@jacobtylerwalls jacobtylerwalls force-pushed the subquery-resolution branch 3 times, most recently from 9740fa8 to 8cd60ac Compare May 12, 2025 11:56
Comment on lines 134 to 139
def as_oracle(self, compiler, connection):
"""
Regardless of whether a subquery has been limited to return
a single row, this error is raised for <, <=, >, and >=:
ORA-01796: this operator cannot be used with lists
"""
Copy link
Member Author
@jacobtylerwalls jacobtylerwalls May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An earlier version of this PR aimed to guard for subqueries inside get_fallback_sql(), since I was seeing Query object is not iterable on for val in self.rhs, but even the versions of Oracle that support tuple lookups throw this ORA-01796 error, so I just decided to guard oracle. If there are other vendors with get_fallback_sql() methods, they may need adjusting to handle subqueries. Open to ideas here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a bit of time with the latest versions of Oracle so I have no ideas to offer.

@jacobtylerwalls jacobtylerwalls changed the title Fixed #36210 -- Allowed Subquery usage in futher lookups against composite pks. Fixed #36210 -- Allowed Subquery usage in further lookups against composite pks. May 12, 2025
@jacobtylerwalls

This comment was marked as outdated.

Comment on lines 134 to 139
def as_oracle(self, compiler, connection):
"""
Regardless of whether a subquery has been limited to return
a single row, this error is raised for <, <=, >, and >=:
ORA-01796: this operator cannot be used with lists
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a bit of time with the latest versions of Oracle so I have no ideas to offer.

@jacobtylerwalls

This comment was marked as outdated.

@jacobtylerwalls

This comment was marked as outdated.

…posite pks.

Follow-up to 8561100.

co-authored-by: Simon Charette <charette.s@gmail.com>
Comment on lines 127 to +128
def as_sql(self, compiler, connection):
if (
Copy link
Member Author
@jacobtylerwalls jacobtylerwalls May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oracle 19 was failing because the two mixins (TupleLookupMixin and TupleComparisonMixin were not ordered correctly). Rather than have two mixins, which seems a little fragile, I just brought the new logic here, and guarded with an isinstance check. Seems like a pick your thorn thing. 🤷‍♂️

@jacobtylerwalls
Copy link
Member Author

buildbot, test on oracle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0