#2210: [patch] order_by on related table with db_column different to name fails
------------------------------------------------+---------------------------
   Reporter:  Russell Cloran <[EMAIL PROTECTED]>  |                Owner:  
adrian          
     Status:  new                               |            Component:  
Database wrapper
    Version:  SVN                               |           Resolution:         
         
   Keywords:  order_by related                  |                Stage:  
Accepted        
  Has_patch:  1                                 |           Needs_docs:  0      
         
Needs_tests:  1                                 |   Needs_better_patch:  1      
         
------------------------------------------------+---------------------------
Comment (by anonymous):

 Sorry, comment_count should have been "squeek".
 
 That said, I had to un-apply the patch and work with the extra( ) to get
 it all working without the patch as I'm on a very tight deadline. However
 here's what I found the last time:
 
 Models:
 
 {{{
 from django.db import models
 
 class Book(models.Model):
     title = models.TextField()
 
 class BookStat(models.Model):
     book = models.OneToOneField(Book)
     times_read = models.PositiveIntegerField()
 
 class BookRating(models.Model):
     book = models.ForeignKey(Book)
     rating = models.PositiveIntegerField()
 }}}
 
 Shell:
 {{{
 from example.models import *
 
 bfg = Book.objects.create(title="bfg")
 bfg.save()
 matilda = Book.objects.create(title="matilda")
 matilda.save()
 
 BookRating.objects.create(book=bfg, rating=5).save()
 BookRating.objects.create(book=bfg, rating=8).save()
 BookRating.objects.create(book=bfg, rating=15).save()
 BookRating.objects.create(book=matilda, rating=15).save()
 BookRating.objects.create(book=matilda, rating=16).save()
 BookRating.objects.create(book=matilda, rating=17).save()
 BookStat.objects.create(book=bfg, times_read=5).save()
 BookStat.objects.create(book=matilda, times_read=3).save()
 
 # works with patch:
 qs = Book.objects.all().order_by("stats__times_read")
 
 # the equivalent, works without patch, same result
 qs = qs.extra(where=["example_book.id = example_bookstat.book_id"],
 tables=["example_bookstat"]).order_by("-example_bookstat.times_read")
 
 # works WITHOUT patch, breaks with patch
 qs = Book.objects.all().extra(select={ 'avg_rating': 'SELECT avg(rating)
 FROM example_bookrating WHERE example_bookrating.book_id =
 example_book.id'}).order_by("-avg_rating")
 }}}
 
 Right now I can't do the stack-trace as I would have to re-apply the
 patch...
 
 For people looking into these problems, this is a workaround for the
 initial problem described in the ticket:

-- 
Ticket URL: <http://code.djangoproject.com/ticket/2210#comment:18>
Django Code <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