Skip to content

Commit

Permalink
Add a potential fix^W^Wterrible hack for EXISTS() subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Apr 2, 2024
1 parent 2814885 commit 389094c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
12 changes: 1 addition & 11 deletions tests/testapp/test_queries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest import expectedFailure

from django import forms
from django.core.exceptions import ValidationError
from django.db import connections, models
Expand Down Expand Up @@ -479,16 +477,8 @@ def test_reference(self):
[references.child2_1, references.child2_2],
)

def test_reference_isnull(self):
self.assertSequenceEqual(
Model.objects.with_tree_fields().filter(referencemodel__isnull=True), []
)
self.assertSequenceEqual(
Model.objects.with_tree_fields().exclude(referencemodel__isnull=True), []
)

@expectedFailure
def test_reference_isnull_issue63(self):
# https://github.com/feincms/django-tree-queries/issues/63
self.assertSequenceEqual(
Model.objects.with_tree_fields().exclude(referencemodel__isnull=False), []
)
Expand Down
5 changes: 5 additions & 0 deletions tree_queries/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ def get_sibling_order_params(self):
return ordering_params

def as_sql(self, *args, **kwargs):
# Try detecting if we're used in a EXISTS() subquery; we do not need
# the tree table in that case. See GitHub issue #63.
if self.query.subquery and self.query.annotation_select_mask == ["a"]:
return super().as_sql(*args, **kwargs)

# The general idea is that if we have a summary query (e.g. .count())
# then we do not want to ask Django to add the tree fields to the query
# using .query.add_extra. The way to determine whether we have a
Expand Down

0 comments on commit 389094c

Please sign in to comment.