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.

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

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