#34262: Queryset grouped by annotation with aggregates on another annotated
expression crashes on MySQL with sql_mode=only_full_group_by.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: ontowhee
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: mysql | Triage Stage: Accepted
only_full_group_by |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ontowhee):
* owner: (none) => ontowhee
* status: new => assigned
Comment:
Should a new ticket be created for adding support for ANY_VALUE()? For
MySQL, since ANY_VALUE() is not an aggregate function, would it be
implemented as an Aggregate or Func or a different expression type?
Just thinking out loud here. If option 2 is the path forward,
- Would the warning be raised before the query is evaluated? That means
the query needs to detect if there are nonaggregated expressions. I'll
need to dig more to understand how that might work.
- Or, raise the warning after the query is evaluated by catching the
OperationalError in the
[https://github.com/django/django/blob/main/django/db/backends/mysql/base.py#L80
CursorWrapper]?
{{{
codes_for_warnings = (
1055, # Expression not in GROUP BY and contains nonaggregated
colum
)
...
def execute(self, query, args=None):
try:
# args is None means no string interpolation
return self.cursor.execute(query, args)
except Database.OperationalError as e:
# Map some error codes to IntegrityError, since they seem to
be
# misclassified and Django would prefer the more logical
place.
if e.args[0] in self.codes_for_integrityerror:
raise IntegrityError(*tuple(e.args))
else if e.args[0] in self.codes_for_warnings:
warnings.warn(
"%s "
"Consider wrapping the nonaggregated expression using
AnyValue." %(e),
RuntimeWarning,
)
return
raise
}}}
I'm going to dig around and see if the warning can be raised before the
query is evaluated. I might start looking through the resolve_expression()
functions, since there seem to be patterns of raising errors there.
--
Ticket URL: <https://code.djangoproject.com/ticket/34262#comment:14>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070194e329d1fe-83a86a2e-6e7b-4ff8-8420-64f05d14337b-000000%40eu-central-1.amazonses.com.