#11568: Ordering by related attributes makes distinct() break
------------------------------------------+---------------------------------
 Reporter:  sfllaw                        |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  1.0       
 Keywords:  order_by distinct ForeignKey  |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 The problem is that sorting on an attribute of a related model causes
 ```distinct()``` to fail.

 This is because the ```SELECT DISTINCT``` operates on the extra column
 required for the LEFT OUTER JOIN, which makes it impossible to group the
 desired results.

 To reproduce the error, run this test case:

 '''models.py'''
 {{{
 import django

 from django.db import models


 class Article(models.Model):
     title = models.CharField(max_length=128)

     def __unicode__(self):
         return self.title

 class Comment(models.Model):
     article = models.ForeignKey(Article)
     comment = models.CharField(max_length=128)

     def __unicode__(self):
         return "%s on %s" % (self.comment, self.article.title)

 }}}

 '''tests.py'''
 {{{
 from django.test import TestCase
 from django import db
 from django.conf import settings

 from .models import Article, Comment


 class TestOrderDistinctBug(TestCase):
     def test(self):
         settings.DEBUG = True
         awesome = Article.objects.create(title="Awesome Title")
         different = Article.objects.create(title="Different Title")
         wonderfull = Article.objects.create(title="Bad Title")

         Comment.objects.create(article=awesome, comment="Agreed")
         Comment.objects.create(article=awesome, comment="Dummy")
         Comment.objects.create(article=different, comment="Yeah")
         Comment.objects.create(article=different, comment="Onoz ")
         Comment.objects.create(article=wonderfull, comment="First !")

         db.reset_queries()
         articles = Article.objects.order_by("comment__comment").distinct()

         print "articles=%s" % articles
         # articles=[<Article: Awesome Title>, <Article: Awesome Title>,
 <Article: Bad Title>, <Article: Different Title>, <Article: Different
 Title>]

         print "sql is : %s" % db.connection.queries[0]
         # sql is : {'time': '0.001',
         # 'sql': u'SELECT DISTINCT `order_bug_article`.`id`,
 `order_bug_article`.`title`, `order_bug_comment`.`comment`
         # FROM `order_bug_article` LEFT OUTER JOIN `order_bug_comment` ON
 (
         #    `order_bug_article`.`id` = `order_bug_comment`.`article_id`
         # ) ORDER BY `order_bug_comment`.`comment` ASC LIMIT 21'}


         # count returns the right thing
         self.assertEquals(articles.count(), 3)
         # but the content of the query set is wrong
         self.assertEquals(len(articles), 3)
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11568>
Django <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to