#13123 closed (duplicate)
Extra params + aggregation creates incorrect SQL.
| Reported by: | Paul Garner | Owned by: | nobody |
|---|---|---|---|
| Component: | Uncategorized | Version: | 1.2-beta |
| Severity: | Keywords: | ||
| Cc: | ego@… | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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).
Attachments (1)
Change History (9)
by , 16 years ago
| Attachment: | compiler.diff added |
|---|
comment:1 by , 16 years ago
| Cc: | added |
|---|
comment:2 by , 16 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
comment:4 by , 16 years ago
| Resolution: | duplicate → fixed |
|---|
comment:5 by , 16 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
This is almost certainly the wrong ticket.
comment:6 by , 16 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | reopened → closed |
But it's still a dupe(I couldn't change the resolution).
comment:7 by , 16 years ago
I agree, that changeset has nothing to do with this bug or the one it's a dupe of.
If it's the same problem as another already open one, just attach the patch to the open ticket. A committer will take care of ensuring the ticket is fixed on current trunk and the most recent release branch, if applicable and possible.