#25307: Cannot use .annotate with conditional expressions
-------------------------------------+-------------------------------------
Reporter: jproffitt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by freshquiz):
Please correct me if I'm wrong, but it seems as if the issue lies with the
`django.db.models.expressions.When` class, in that the `condition`
attribute is supposed to be a `django.db.models.Q` object and yet the
`get_source_epxressions()` and `set_source_expressions()` methods are
treating the `condition` attribute as an
`django.db.models.expressions.Expression`, when it most certainly is not.
This violates the API and hence client-code of the API (such as
aggregation code in this case) will fail unexpectedly.
E.g.
In the example provided by @martsberger, the `count_recent` expression
gets down to `django.db.models.sql.query.Query.rewrite_cols()`. That
method recursively travels into the nested expressions until it hits the
`Q` object returned by the `When` expression's `get_source_expressions()`
and that's where it fails.
The problem is only triggered (during aggregation) by column rewriting,
when this line evaluates to `True` in
`django.db.models.sql.query.Query.get_aggregation()`:
{{{
if (isinstance(self.group_by, list) or has_limit or
has_existing_annotations or self.distinct):
}}}
As I said initially, I believe the root cause is the violation of the
`Expression` API by the `When` subclass.
--
Ticket URL: <https://code.djangoproject.com/ticket/25307#comment:5>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.a62606b6e095813839d020267177ecc8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.