summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-10-24 06:09:47 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-10-24 06:09:47 +0000
commit9319dc496c5f8d215a293fff5fb974e48e68454d (patch)
treeb3cc670ec3aaf79ff709001349a16c055f149a21 /django/db/models/sql/query.py
parente3aa9a28288737a75f05f2174b4105d01e52af96 (diff)
Fixed #9406 -- Ensure that each database column is only represented once in the
"ORDER BY" clause of an SQL statement. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9251 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 01fbd3d002..ee63a0214f 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -621,6 +621,12 @@ class Query(object):
asc, desc = ORDER_DIR['ASC']
else:
asc, desc = ORDER_DIR['DESC']
+
+ # It's possible, due to model inheritance, that normal usage might try
+ # to include the same field more than once in the ordering. We track
+ # the table/column pairs we use and discard any after the first use.
+ processed_pairs = set()
+
for field in ordering:
if field == '?':
result.append(self.connection.ops.random_function_sql())
@@ -638,18 +644,22 @@ class Query(object):
# on verbatim.
col, order = get_order_dir(field, asc)
table, col = col.split('.', 1)
- elt = '%s.%s' % (qn(table), col)
- if not distinct or elt in select_aliases:
- result.append('%s %s' % (elt, order))
+ if (table, col) not in processed_pairs:
+ elt = '%s.%s' % (qn(table), col)
+ processed_pairs.add((table, col))
+ if not distinct or elt in select_aliases:
+ result.append('%s %s' % (elt, order))
elif get_order_dir(field)[0] not in self.extra_select:
# 'col' is of the form 'field' or 'field1__field2' or
# '-field1__field2__field', etc.
for table, col, order in self.find_ordering_name(field,
self.model._meta, default_order=asc):
- elt = '%s.%s' % (qn(table), qn2(col))
- if distinct and elt not in select_aliases:
- ordering_aliases.append(elt)
- result.append('%s %s' % (elt, order))
+ if (table, col) not in processed_pairs:
+ elt = '%s.%s' % (qn(table), qn2(col))
+ processed_pairs.add((table, col))
+ if distinct and elt not in select_aliases:
+ ordering_aliases.append(elt)
+ result.append('%s %s' % (elt, order))
else:
col, order = get_order_dir(field, asc)
elt = qn2(col)