Skip to content

Conversation

@CelestialGuru
Copy link

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.

@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

Oring Exists() subqeries together "Exists() | Exists()" creates a Q instance, but not one which has Q node children.
@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

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 75.35%. Comparing base (0d820d0) to head (acf288a).

Files with missing lines Patch % Lines
src/polymorphic/query_translate.py 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #572      +/-   ##
==========================================
- Coverage   75.40%   75.35%   -0.06%     
==========================================
  Files          21       21              
  Lines        1342     1343       +1     
  Branches      211      212       +1     
==========================================
  Hits         1012     1012              
  Misses        257      257              
- Partials       73       74       +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.

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.

3 participants