﻿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
25796	annotate() exception if there are same fields name OR in Q	Falldog	nobody	"I had try a complete annotate, got this issue
Fail on MySQL, good on SQLite

here is example models

{{{
class Shipment(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32, null=True)  # => field name same as below

class SubShipment(models.Model):
    id = models.AutoField(primary_key=True)
    primary_shipment = models.ForeignKey(Shipment, related_name='sub_shipment_set')
    name = models.CharField(max_length=32, null=True)  # => field `name` same as ForeignKey(Shipment).`name`

class Commodity(models.Model):
    id = models.AutoField(primary_key=True)
    shipment = models.ForeignKey(Shipment, related_name='commodity_set')
    description = models.CharField(max_length=128)
}}}

here is query example
only exception on Q() by `OR` on the same field name condition, `name` and `sub_shipment_set__name`
I try Q() `AND` is okay

{{{
    query = models.Shipment.objects.annotate(commodity_count=Count('commodity_set'))

    # OR all condition
    q = Q(commodity_count=0)
    q |= Q(name__isnull=False) | Q(sub_shipment_set__name__isnull=False)  # query same `name` will cause exception

    query = query.filter(q)
}}}

got exception below
{{{
(1054, ""Unknown column 'myapp_subshipment.name' in 'having clause'"")
}}}


----
I had dump SQL query below

{{{
SELECT `myapp_shipment`.`id`,
       `myapp_shipment`.`name`,
       COUNT(`myapp_commodity`.`id`) AS `commodity_count`
FROM `myapp_shipment`
LEFT OUTER JOIN `myapp_commodity` ON (`myapp_shipment`.`id` = `myapp_commodity`.`shipment_id`)
LEFT OUTER JOIN `myapp_subshipment` ON (`myapp_shipment`.`id` = `myapp_subshipment`.`primary_shipment_id`)
GROUP BY `myapp_shipment`.`id`,
         `myapp_shipment`.`name`,
         `myapp_subshipment`.`name`
HAVING (COUNT(`myapp_commodity`.`id`) = 0
        OR `myapp_shipment`.`name` IS NOT NULL
        OR `myapp_subshipment`.`name` IS NOT NULL)
ORDER BY NULL
}}}

for MySQL query command `GROUP BY` on  `myapp_subshipment`.`name`
it must appear in `SELECT` area
I try to put it into `SELECT` area and execute query again, it's okay
"	Bug	closed	Database layer (models, ORM)	dev	Normal	invalid	annotate		Unreviewed	0	0	0	0	0	0
