Repository navigation
Conversation
dbaf23b to
6e653c4
Compare
|
Two things I'd like to confirm, plus a test gap. 1. The old code did: Now that block is replaced with
If a non-aggregate model select is still present, Could you confirm whether 2. Test gaps The current tests cover relation-filter and m2m-filter aggregates, plus the per-object case. Missing:
Minor: readability of the The timing works because |
6e653c4 to
5c7b4e1
Compare
|
Thanks for the review!
Minor. Reworded the comment on Ready for another look whenever you have time @waketzheng ! |
Description
Skips the automatic
GROUP BYon the model columns whenvalues()/values_list()select only aggregates and the only joins come from filters:Without the relation filter, the same query already returns one row. Joins from the annotations, the selected fields or the ordering still group, so
Author.annotate(n=Count("books")).values("n")stays per author. Model querysets,count(),exists(), and queries with aHAVINGor anORDER BYkeep the current grouping.ValuesQuerynow passes its selected fields toresolve_filters(), likeValuesListQueryalready does.Through a reverse FK or m2m filter, the join repeats each object once per matching row, so the aggregate now sees those rows:
Event.filter(participants__id__in=[...]).annotate(n=Count("event_id")).values("n")returns[{'n': 4}]instead of[{'n': 3}, {'n': 1}], andCount("event_id", distinct=True)counts the events ([{'n': 2}]). This is what the same SQL returns without the ORM.Motivation and Context
Closes #613, closes #1149. A total over rows filtered on a relation came back as one row per object, because the filter's join triggered the
GROUP BYmeant for per-object aggregates over a relation.How Has This Been Tested?
New tests in
tests/test_aggregation.py, run on SQLite and PostgreSQL 18 (asyncpg). The full suite passes on SQLite and asyncpg. MySQL and MSSQL are left to CI.Checklist:
This fix has been partially implemented by Claude Opus 5.5. All modifications made by the AI have been reviewed by me.