#13123: Extra params + aggregation creates incorrect SQL.
---------------------------+------------------------------------------------
 Reporter:  anentropic     |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.2-beta  
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  1              |  
---------------------------+------------------------------------------------
 If you have a query with both annotation/aggregation (and thus a GROUP BY
 clause) *and* an extra(select) part ...the generated sql copies the whole
 subselect from the extra field into the group by portion of the query,
 when just the column alias would be correct.

 To illustrate, a queryset like this:
 {{{
 qs.annotate(stock=Sum('variants__stock')).filter(Q(stock__gt=0)).extra(
     select={'op_option_group_id': '''
         SELECT op.option_group_id FROM store_optionedproduct op,
 store_product WHERE op.product_ptr_id=store_product.id LIMIT 1
     '''})
 }}}

 generates this sql:
 {{{
 SELECT
     (SELECT op.option_group_id FROM store_optionedproduct op,
 store_product WHERE op.product_ptr_id=store_product.id) AS
 `op_option_group_id`
     FROM `store_product`
     LEFT OUTER JOIN `store_productvariant` ON
         (`store_product`.`id` = `store_productvariant`.`product_id`)
         WHERE (`store_product`.`status` IN (1))
         GROUP BY `store_product`.`id`,
 -->     SELECT op.option_group_id FROM store_optionedproduct op,
 store_product WHERE op.product_ptr_id=store_product.id
         HAVING SUM(`store_productvariant`.`stock`) > 0 ORDER BY NULL
 }}}

 when it should be:
 {{{
 SELECT
     (SELECT op.option_group_id FROM store_optionedproduct op,
 store_product WHERE op.product_ptr_id=store_product.id) AS
 `op_option_group_id`
     FROM `store_product`
     LEFT OUTER JOIN `store_productvariant` ON
         (`store_product`.`id` = `store_productvariant`.`product_id`)
         WHERE (`store_product`.`status` IN (1))
         GROUP BY `store_product`.`id`,
 -->     `op_option_group_id`
         HAVING SUM(`store_productvariant`.`stock`) > 0 ORDER BY NULL
 }}}

 This is the same as #11916 but I've made this duplicate specifically for
 Django 1.2, as a different patch is needed (attached).

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13123>
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