﻿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
24748	GROUP BY clause incorrect with foreign key to self in MySQL	Ben Buchwald	Anssi Kääriäinen	"A query I had that previously worked in Django 1.7 is quite broken in 1.8. When annotating a query of a model that has a a foreign key to itself, the wrong field is grouped by. This only occurs in MySQL due a bug to the group by primary key optimization.

Using this example model:
{{{
class Item(Model):
    name = CharField(max_length=50)
    parent = ForeignKey('self',null=True,blank=True,related_name='children')
}}}

I used to be able to get a list of items with their number of children like this:
{{{Item.objects.all().annotate(num_children=Count('children'))}}}

But in Django 1.8, this groups by parent_id instead of by id. This is because both are selected, and the output field for both are the same, the primary key of Item."	Bug	closed	Database layer (models, ORM)	1.8	Release blocker	fixed	annotate mysql	josh.smeaton@…	Ready for checkin	1	0	0	0	0	0
