#14137: Django ORM adding unwanted parens when using extra()
---------------------------+------------------------------------------------
 Reporter:  mjs7231        |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.2       
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  0              |  
---------------------------+------------------------------------------------
 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;
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14137>
Django <http://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 post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to