Repository navigation
fix(query): count and restrict expressions with an empty primary key - #1605
Open
dimitri-yatsenko wants to merge 1 commit into
Open
dimitri-yatsenko wants to merge 1 commit into
dimitri-yatsenko wants to merge 1 commit into
Conversation
An expression with an empty primary key, such as `dj.U() & T` or `dj.U().aggr(T, n="count(*)")`, holds at most one row. Two operations on such expressions failed: - `len()` took the single-column branch and generated `count(DISTINCT )`, which is invalid SQL. It now returns 0 or 1 by checking whether the row exists. - Restricting a `dj.U()` aggregation hit a bare assert in `Aggregation.make_sql`. Removing the assert alone would not have been enough: the MySQL branch emitted HAVING only together with GROUP BY, so the restriction would have been dropped without notice. MySQL now emits HAVING without GROUP BY, and PostgreSQL takes the subquery path whenever the aggregation is restricted, with or without grouping attributes. Restriction conditions on an aggregation are now joined with str(), as the base where_clause already does, so restricting any aggregation by False no longer raises TypeError. Closes #1581 Closes #1580
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
October 17, 2026 22:02
This branch has not been deployed
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.
An expression with an empty primary key, such as
dj.U() & Tordj.U().aggr(T, n="count(*)"), holds at most one row. Two operations onsuch expressions failed:
len()took the single-column branch and generatedcount(DISTINCT ), which is invalid SQL. It now returns 0 or 1 bychecking whether the row exists.
dj.U()aggregation hit a bare assert inAggregation.make_sql. Removing the assert alone would not have beenenough: the MySQL branch emitted HAVING only together with GROUP BY,
so the restriction would have been dropped without notice. MySQL now
emits HAVING without GROUP BY, and PostgreSQL takes the subquery path
whenever the aggregation is restricted, with or without grouping
attributes.
Restriction conditions on an aggregation are now joined with str(), as
the base where_clause already does, so restricting any aggregation by
False no longer raises TypeError.
Closes #1581
Closes #1580