﻿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
14139	Feature Request: distinct() should support field names	mjs7231		"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 below.  However the resulting query adds parens around the extra() clause:
{{{
queryset = Report.objects.extra(select={'lastid':""DISTINCT ON (user_id) id""})
queryset = queryset.order_by('user', '-date')
queryset = queryset.values('lastid')
}}}


According to bug #1413, this is the wrong approach.  A better solution is that distinct() should support field names:

''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.''





"		closed	Database layer (models, ORM)	1.2		duplicate			Design decision needed	0	0	0	0	0	0
