#14137 closed (invalid)
Django ORM adding unwanted parens when using extra()
Reported by: | mjs7231 | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 1.1 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a table REPORTS containing the columns {id, user, date}. I was attempting to get the row with the largest date for each user. Using Django's ORM I can pretty much get what I want using the queryset definition:
queryset = Report.objects.extra(select={'lastid':"DISTINCT ON (user_id) id"}) queryset = queryset.order_by('user', '-date') queryset = queryset.values('lastid')
This results in the query:
SELECT (DISTINCT ON (user_id) id) AS "lastid" FROM "report" ORDER BY "report"."user_id" ASC, "report"."date" DESC;
Postgres spits out an exception with this query. However, if you remove the extra parens around the DISTINCT clause, it works fine. So the query should really look like:
SELECT DISTINCT ON (user_id) id AS "lastid" FROM "report" ORDER BY "report"."user_id" ASC, "report"."date" DESC;
Change History (3)
comment:1 by , 14 years ago
Version: | 1.2 → 1.1 |
---|
comment:2 by , 14 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
The "select" keyword in
extra()
should be used to select extra columns which can't be expressed in ORM (like complex expressions, using stored procedures, stuff like that) not to inject arbitrary SQL into a query. A better solution would be to support field names in distinct(), but that's a diffrent thing. After all, there is no reason why the ORM wouldn't put extra select columns on the end, which would also break your code.