#11387: order_by over a GenericRelation generates incorrect SQL
---------------------------------------------------+------------------------
          Reporter:  adurdin                       |         Owner:  mtredinnick
            Status:  new                           |     Milestone:             
         Component:  Database layer (models, ORM)  |       Version:  SVN        
        Resolution:                                |      Keywords:             
             Stage:  Design decision needed        |     Has_patch:  0          
        Needs_docs:  0                             |   Needs_tests:  1          
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by adurdin):

  * needs_better_patch:  => 0
  * stage:  Unreviewed => Design decision needed
  * needs_tests:  => 1
  * needs_docs:  => 0

Comment:

 I’ve been thinking about the problem some more, and that test is wrong,
 and here’s why:

 If you order_by across a reverse ForeignKey relation, a LEFT JOIN is
 created to the target table.  So if you order_by across a reverse
 GenericForeignKey relation, it should also create a LEFT JOIN to the
 target table.

 There is a deeper problem here, I believe.  The correct query for this
 case should be:
 {{{
 SELECT "foo_foo"."id" FROM "foo_foo" LEFT OUTER JOIN "foo_bar" ON
 ("foo_foo"."id" = "foo_bar"."object_id" AND "foo_bar"."content_type_id" =
 %s) ORDER BY "foo_bar"."id" ASC', (8,))
 }}}

 But as far as I have been able to determine, the query generation does not
 support multiple join conditions, and I don't believe it is possible to
 transform the above query to move the second condition to the WHERE
 clause.  This means that the query generation code will have to be amended
 in numerous places to support multiple join conditions.  I fear patching
 this is beyond me.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11387#comment:1>
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