Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cacheops/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,13 @@ def table_for(alias):

# Add any subqueries used for annotation
if qs.query.annotations:
subqueries = (query_dnf(getattr(q, 'query', None))
for q in qs.query.annotations.values() if isinstance(q, Subquery))
dnfs_.update(join_with(lcat, subqueries))
sub_dnfs = []
for q in qs.query.annotations.values():
if isinstance(q, Subquery):
sub_dnfs.append(query_dnf(getattr(q, 'query', None)))
elif isinstance(q, Query):
sub_dnfs.append(query_dnf(q))
dnfs_.update(join_with(lcat, sub_dnfs) or {})

return dnfs_

Expand Down
12 changes: 12 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,18 @@ def test_365(self):
categories = Category.objects.cache().annotate(newest_post=Subquery(newest_post[:1]))
self.assertEqual(categories[0].newest_post, post.pk)

def test_366(self):
"""
Check that dnfs() detects table dependencies from Subquery annotations.
Django 6.0+ resolves Subquery into raw Query objects in qs.query.annotations, so this
verifies detection works regardless of how the annotation is stored.
"""
from cacheops.tree import dnfs

newest_post = Post.objects.filter(category=OuterRef('pk')).order_by('-pk').values('pk')
qs = Category.objects.cache().annotate(newest_post=Subquery(newest_post[:1]))
self.assertEqual(dnfs(qs), {Category._meta.db_table: [{}], Post._meta.db_table: [{}]})

@unittest.skipIf(platform.python_implementation() == "PyPy", "dill doesn't do that in PyPy")
def test_385(self):
Client.objects.create(name='Client Name')
Expand Down
Loading