Skip to content

Commit

Permalink
Fixed #7371 -- Fixed an edge case when ordering on related models.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7786 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Jun 29, 2008
1 parent c17e326 commit 050d0a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions django/db/models/sql/query.py
Expand Up @@ -626,6 +626,11 @@ def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
alias, False)
alias = joins[-1]
col = target.column
if not field.rel:
# To avoid inadvertent trimming of a necessary alias, use the
# refcount to show that we are referencing a non-relation field on
# the model.
self.ref_alias(alias)

# Must use left outer joins for nullable fields.
for join in joins:
Expand Down
15 changes: 15 additions & 0 deletions tests/regressiontests/queries/models.py
Expand Up @@ -172,6 +172,17 @@ class Child(models.Model):
person = models.OneToOneField(Member, primary_key=True)
parent = models.ForeignKey(Member, related_name="children")

# Custom primary keys interfered with ordering in the past.
class CustomPk(models.Model):
name = models.CharField(max_length=10, primary_key=True)
extra = models.CharField(max_length=10)

class Meta:
ordering = ['name', 'extra']

class Related(models.Model):
custom = models.ForeignKey(CustomPk)


__test__ = {'API_TESTS':"""
>>> t1 = Tag.objects.create(name='t1')
Expand Down Expand Up @@ -786,5 +797,9 @@ class Child(models.Model):
>>> n1.annotation_set.filter(Q(tag=t5) | Q(tag__children=t5) | Q(tag__children__children=t5))
[<Annotation: a1>]
Bug #7371
>>> Related.objects.order_by('custom')
[]
"""}

0 comments on commit 050d0a1

Please sign in to comment.