Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#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


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 Changed 8 years ago by mjs7231

Version: 1.21.1

comment:2 Changed 8 years ago by Łukasz Rekucki

Resolution: invalid
Status: newclosed

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.

comment:3 Changed 8 years ago by mjs7231

OK, filed a Feature Request with Ticket #14139.

Note: See TracTickets for help on using tickets.
Back to Top