Author: mtredinnick
Date: 2009-02-27 20:59:40 -0600 (Fri, 27 Feb 2009)
New Revision: 9916

Modified:
   django/trunk/django/db/models/sql/query.py
   django/trunk/tests/regressiontests/queries/models.py
Log:
Fixed #10028 -- Fixed a problem when ordering by related models.

Some results were inadvertently being excluded if we were ordering across a
nullable relation which itself ordering by a non-nullable relation.

Modified: django/trunk/django/db/models/sql/query.py
===================================================================
--- django/trunk/django/db/models/sql/query.py  2009-02-28 01:29:00 UTC (rev 
9915)
+++ django/trunk/django/db/models/sql/query.py  2009-02-28 02:59:40 UTC (rev 
9916)
@@ -716,7 +716,7 @@
         result, params = [], []
         if self.group_by is not None:
             group_by = self.group_by or []
-            
+
             extra_selects = []
             for extra_select, extra_params in self.extra_select.itervalues():
                 extra_selects.append(extra_select)
@@ -834,8 +834,9 @@
             # the model.
             self.ref_alias(alias)
 
-        # Must use left outer joins for nullable fields.
-        self.promote_alias_chain(joins)
+        # Must use left outer joins for nullable fields and their relations.
+        self.promote_alias_chain(joins,
+                self.alias_map[joins[0]][JOIN_TYPE] == self.LOUTER)
 
         # If we get to this point and the field is a relation to another model,
         # append the default ordering for that model.

Modified: django/trunk/tests/regressiontests/queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/queries/models.py        2009-02-28 
01:29:00 UTC (rev 9915)
+++ django/trunk/tests/regressiontests/queries/models.py        2009-02-28 
02:59:40 UTC (rev 9916)
@@ -238,7 +238,33 @@
 class PointerB(models.Model):
     connection = models.ForeignKey(SharedConnection)
 
+# Multi-layer ordering
+class SingleObject(models.Model):
+    name = models.CharField(max_length=10)
 
+    class Meta:
+        ordering = ['name']
+
+    def __unicode__(self):
+        return self.name
+
+class RelatedObject(models.Model):
+    single = models.ForeignKey(SingleObject)
+
+    class Meta:
+        ordering = ['single']
+
+class Plaything(models.Model):
+    name = models.CharField(max_length=10)
+    others = models.ForeignKey(RelatedObject, null=True)
+
+    class Meta:
+        ordering = ['others']
+
+    def __unicode__(self):
+        return self.name
+
+
 __test__ = {'API_TESTS':"""
 >>> t1 = Tag.objects.create(name='t1')
 >>> t2 = Tag.objects.create(name='t2', parent=t1)
@@ -1044,6 +1070,11 @@
 >>> Annotation.objects.filter(notes__in=Note.objects.filter(note="n1").values_list('note').values('id'))
 [<Annotation: a1>]
 
+Bug #10028 -- ordering by model related to nullable relations(!) should use
+outer joins, so that all results are included.
+>>> _ = Plaything.objects.create(name="p1")
+>>> Plaything.objects.all()
+[<Plaything: p1>]
 """}
 
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__


--~--~---------~--~----~------------~-------~--~----~
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