#24748: GROUP BY clause incorrect with foreign key to self in MySQL
----------------------------------------------+----------------------------
     Reporter:  sparkyb                       |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8
     Severity:  Normal                        |   Keywords:  annotate mysql
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+----------------------------
 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24748>
Django <https://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 unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.11c084993edde5e068186e8075178d56%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to