﻿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
13123	Extra params + aggregation creates incorrect SQL.	Paul Garner	nobody	"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)."		closed	Uncategorized	1.2-beta		duplicate		ego@…	Unreviewed	1	0	0	0	0	0
