From f55b834c2f49a2e07e38a5c8ec7e88a8237ab98e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 1 Sep 2008 12:07:26 +0000 Subject: [PATCH] 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 --- django/db/models/sql/query.py | 2 +- tests/modeltests/ordering/models.py | 9 +++++++++ tests/regressiontests/queries/models.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 2e3169a3c0..9e67e9a993 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -629,7 +629,7 @@ class Query(object): 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)) diff --git a/tests/modeltests/ordering/models.py b/tests/modeltests/ordering/models.py index 94d24b352d..a53d93c330 100644 --- a/tests/modeltests/ordering/models.py +++ b/tests/modeltests/ordering/models.py @@ -77,4 +77,13 @@ __test__ = {'API_TESTS':""" # take the first two). >>> Article.objects.all().reverse()[:2] [, ] + +# Ordering can be based on fields included from an 'extra' clause +>>> Article.objects.extra(select={'foo': 'pub_date'}, order_by=['foo', 'headline']) +[, , , ] + +# 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']) +[, , , ] + """} diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index d96f1de859..31c6b8d52b 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -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 @@ Bug #8597: regression tests for case-insensitive comparisons >>> Item.objects.filter(name__iendswith="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.objects.extra(select={'stuff':'name'}, order_by=('order','stuff')) +[, ] + """} # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__