Fixed #7302: Corrected quoting of columns in extra_group_by. Thanks to Ivan Sagalaev for the patch and initial test.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8794 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2008-09-01 12:07:26 +00:00
parent 823263429f
commit f55b834c2f
3 changed files with 26 additions and 1 deletions

View File

@ -629,7 +629,7 @@ class Query(object):
result.append('%s %s' % (elt, order)) result.append('%s %s' % (elt, order))
else: else:
col, order = get_order_dir(field, asc) col, order = get_order_dir(field, asc)
elt = qn(col) elt = qn2(col)
if distinct and elt not in select_aliases: if distinct and elt not in select_aliases:
ordering_aliases.append(elt) ordering_aliases.append(elt)
result.append('%s %s' % (elt, order)) result.append('%s %s' % (elt, order))

View File

@ -77,4 +77,13 @@ __test__ = {'API_TESTS':"""
# take the first two). # take the first two).
>>> Article.objects.all().reverse()[:2] >>> Article.objects.all().reverse()[:2]
[<Article: Article 1>, <Article: Article 3>] [<Article: Article 1>, <Article: Article 3>]
# Ordering can be based on fields included from an 'extra' clause
>>> Article.objects.extra(select={'foo': 'pub_date'}, order_by=['foo', 'headline'])
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
# If the extra clause uses an SQL keyword for a name, it will be protected by quoting.
>>> Article.objects.extra(select={'order': 'pub_date'}, order_by=['order', 'headline'])
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
"""} """}

View File

@ -220,6 +220,13 @@ class Join(models.Model):
a = models.ForeignKey(LeafA) a = models.ForeignKey(LeafA)
b = models.ForeignKey(LeafB) b = models.ForeignKey(LeafB)
class ReservedName(models.Model):
name = models.CharField(max_length=20)
order = models.IntegerField()
def __unicode__(self):
return self.name
__test__ = {'API_TESTS':""" __test__ = {'API_TESTS':"""
>>> t1 = Tag.objects.create(name='t1') >>> t1 = Tag.objects.create(name='t1')
>>> t2 = Tag.objects.create(name='t2', parent=t1) >>> t2 = Tag.objects.create(name='t2', parent=t1)
@ -919,6 +926,15 @@ Bug #8597: regression tests for case-insensitive comparisons
>>> Item.objects.filter(name__iendswith="A_b") >>> Item.objects.filter(name__iendswith="A_b")
[<Item: a_b>] [<Item: a_b>]
Bug #7302: reserved names are appropriately escaped
>>> _ = ReservedName.objects.create(name='a',order=42)
>>> _ = ReservedName.objects.create(name='b',order=37)
>>> ReservedName.objects.all().order_by('order')
[<ReservedName: b>, <ReservedName: a>]
>>> ReservedName.objects.extra(select={'stuff':'name'}, order_by=('order','stuff'))
[<ReservedName: b>, <ReservedName: a>]
"""} """}
# In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__ # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__