#24431: Combining extra(), annotate() or distinct() and count() can generate
invalid SQL
-------------------------------------+-------------------------------------
Reporter: briankrznarich | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8beta1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: extra distinct | Triage Stage: Accepted
aggregate count annotate |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jarshwah):
For your example here you're able to use .annotate() rather than .extra(),
and it'll do the right thing:
{{{
In [7]: qs =
Permission.objects.select_related('content_type').annotate(foo=F('content_type__app_label')).order_by().values('foo')
In [8]: print(qs.query)
SELECT "django_content_type"."app_label" AS "foo" FROM "auth_permission"
INNER JOIN "django_content_type" ON ( "auth_permission"."content_type_id"
= "django_content_type"."id" )
}}}
Now that 1.8 has support for non-aggregate annotations, nearly anything
that .extra(select=..) was used for can be replaced by annotate. This
doesn't help you WRT django < 1.8 though.
The long term solution is probably to use .annotate and custom expressions
where F() and friends don't give you the exact behaviour you're after.
We'd like to reduce the use of extra as much as possible by improving the
use case of custom expressions.
.extra does provide a `tables` argument though:
https://docs.djangoproject.com/en/1.8/ref/models/querysets/#extra
The docs are sparse for its use, and I've never used the tables argument
myself, so I'm not sure if that'll provide you with what you need.
Especially since the recommendation is to place it at the start of the
queryset to ensure its alias.
Do any of the ideas above help you in your particular use cases?
--
Ticket URL: <https://code.djangoproject.com/ticket/24431#comment:6>
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/072.907360b2c1a4256202afe1fbd844bec4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.