Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions cacheops/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,14 @@ 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))
if sub_dnfs:
dnfs_.update(join_with(lcat, sub_dnfs))
Comment thread
audriusm marked this conversation as resolved.
Outdated

return dnfs_

Expand Down
24 changes: 24 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,30 @@ 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])
)
result = dnfs(qs)

self.assertEqual(result, {
Category._meta.db_table: [{}],
Post._meta.db_table: [{}],
})
Comment thread
audriusm marked this conversation as resolved.
Outdated

@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