﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14137	Django ORM adding unwanted parens when using extra()	mjs7231	nobody	"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;
}}}
"		closed	Uncategorized	1.1		invalid			Unreviewed	0	0	0	0	0	0
