Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #7302: Corrected quoting of columns in extra_group_by. Thanks t…

…o Ivan Sagalaev for the patch and initial test.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8794 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f55b834c2f49a2e07e38a5c8ec7e88a8237ab98e 1 parent 8232634
Russell Keith-Magee freakboy3742 authored
2  django/db/models/sql/query.py
View
@@ -629,7 +629,7 @@ def get_ordering(self):
result.append('%s %s' % (elt, order))
else:
col, order = get_order_dir(field, asc)
- elt = qn(col)
+ elt = qn2(col)
if distinct and elt not in select_aliases:
ordering_aliases.append(elt)
result.append('%s %s' % (elt, order))
9 tests/modeltests/ordering/models.py
View
@@ -77,4 +77,13 @@ def __unicode__(self):
# take the first two).
>>> Article.objects.all().reverse()[:2]
[<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>]
+
"""}
16 tests/regressiontests/queries/models.py
View
@@ -220,6 +220,13 @@ class Join(models.Model):
a = models.ForeignKey(LeafA)
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':"""
>>> t1 = Tag.objects.create(name='t1')
>>> t2 = Tag.objects.create(name='t2', parent=t1)
@@ -919,6 +926,15 @@ class Join(models.Model):
>>> Item.objects.filter(name__iendswith="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__
Please sign in to comment.
Something went wrong with that request. Please try again.