Skip to content

Conversation

@CelestialGuru
Copy link
Contributor

@CelestialGuru CelestialGuru commented Jan 31, 2024

Fixes bug when using Exists subqueries which are or-ed together:

SomeModel.objects.filter(
   Exists(RelatedModelA.objects.filter(some_model=OuterRef("pk"))
   | Exists(RelatedModelB.objects.filter(some_model=OuterRef("pk"))
)

This is done usually for performance reasons, but it is a valid queryset. Exist instances ored together like this creates a Q instance, but the children are not themselves Q instances. They are Exists instances.

Fixed #323

@j-antunes
Copy link
Contributor

Thanks for opening a PR. Could you add the following:

  • A unit test to cover your changes
  • In your PR you change else to an elif, could you add another else so that in case none of the conditions are met, it will raise an exception or return something else.
    Thank you

@bckohan bckohan self-assigned this Dec 4, 2025
@bckohan bckohan added the bug label Dec 4, 2025
@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.50%. Comparing base (0d820d0) to head (6372ce1).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #572      +/-   ##
==========================================
+ Coverage   75.40%   75.50%   +0.09%     
==========================================
  Files          21       21              
  Lines        1342     1343       +1     
  Branches      211      212       +1     
==========================================
+ Hits         1012     1014       +2     
  Misses        257      257              
+ Partials       73       72       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bckohan bckohan changed the title Check if child node is Q instance for subquery expressions Support subquery expressions Dec 4, 2025
@bckohan bckohan changed the title Support subquery expressions Support Q expressions that contain subquery expressions Dec 4, 2025
@bckohan bckohan requested review from bckohan and Copilot December 4, 2025 19:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in the polymorphic query translation logic where Q expressions containing subquery expressions (like Exists) were not handled correctly. When Exists subqueries are combined using the | (OR) operator, Django creates a Q object with Exists instances as children, but the old code incorrectly assumed all non-tuple/list children of Q objects were themselves Q objects and attempted to recursively process them.

Key changes:

  • Modified Q object tree traversal to explicitly check if children are Q objects before recursing
  • Added comprehensive test coverage for the Exists subquery scenario with multiple test cases
  • Added related_name to model field to support the new test queries

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/polymorphic/query_translate.py Fixed Q object traversal to only recurse on Q children, not all non-tuple children
src/polymorphic/tests/test_orm.py Added comprehensive test for Exists subqueries combined with OR operator
src/polymorphic/tests/models.py Added related_name="inline_bs" to InlineModelB.plain_a field for test support
src/polymorphic/tests/migrations/0001_initial.py Updated migration to reflect model changes
docs/changelog.rst Added changelog entry (currently commented out)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: CelestialGuru <45701317+CelestialGuru@users.noreply.github.com>
Co-authored-by: Brian Kohan <bckohan@gmail.com>
Copy link
Contributor

@bckohan bckohan left a comment

Choose a reason for hiding this comment

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

This fixes a real bug - I have added tests. Thank you!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bckohan bckohan merged commit 18be22d into jazzband:master Dec 4, 2025
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query translation - tuple check

3 participants