fix Subquery annotation detection in dnfs() for Django 6.0+#508
Merged
Suor merged 3 commits intoSuor:masterfrom Mar 4, 2026
Merged
fix Subquery annotation detection in dnfs() for Django 6.0+#508Suor merged 3 commits intoSuor:masterfrom
Suor merged 3 commits intoSuor:masterfrom
Conversation
Suor
requested changes
Mar 4, 2026
Suor
reviewed
Mar 4, 2026
10fae3c to
ac5468a
Compare
Django 6.0 resolves Subquery annotations into raw Query objects in qs.query.annotations. The existing isinstance(q, Subquery) check misses these, causing cross-table Subquery dependencies to be invisible to cache invalidation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ac5468a to
8d645cb
Compare
Suor
requested changes
Mar 4, 2026
Owner
|
Merged, thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Django 6.0 changed how
Subqueryexpressions are stored inqs.query.annotations. In Django 5.x, annotation values remain asSubqueryinstances. In Django 6.0+, they are resolved into rawdjango.db.models.sql.query.Queryobjects during.annotate().The annotation dependency detection in
tree.dnfs()only checks forSubqueryinstances:On Django 6,
isinstance(q, Subquery)returnsFalsefor the resolvedQueryobjects, so cross-table Subquery dependencies are completely invisible to cache invalidation.This means any queryset with
Subqueryannotations on a cacheops-enabled model won't invalidate its cache when the subqueried table is modified - the cached result stays stale until timeout.How I found this
Came up while upgrading a project from Django 5.2 to 6.0. An endpoint annotating a cached model with a
Subqueryon a different table was returning stale data after writes to the subqueried table. Inspectingdnfs()output confirmed it only reported the primary table - the subqueried table was missing entirely.Fix
Check for both
Subqueryinstances (Django <6) and resolvedQueryobjects (Django 6+) when scanning annotations for table dependencies.The existing
test_365covers this scenario and would fail on Django 6 without this fix.Added
test_366test to verify this fix.