Author: mtredinnick
Date: 2008-10-24 01:11:37 -0500 (Fri, 24 Oct 2008)
New Revision: 9252
Modified:
django/branches/releases/1.0.X/django/db/models/sql/query.py
django/branches/releases/1.0.X/tests/regressiontests/model_inheritance_regress/models.py
Log:
[1.0.X] Fixed #9406 -- Ensure that each database column is only represented
once in the "ORDER BY" clause of an SQL statement.
Backport of r9251 from trunk.
Modified: django/branches/releases/1.0.X/django/db/models/sql/query.py
===================================================================
--- django/branches/releases/1.0.X/django/db/models/sql/query.py
2008-10-24 06:09:47 UTC (rev 9251)
+++ django/branches/releases/1.0.X/django/db/models/sql/query.py
2008-10-24 06:11:37 UTC (rev 9252)
@@ -621,6 +621,12 @@
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 @@
# 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)
Modified:
django/branches/releases/1.0.X/tests/regressiontests/model_inheritance_regress/models.py
===================================================================
---
django/branches/releases/1.0.X/tests/regressiontests/model_inheritance_regress/models.py
2008-10-24 06:09:47 UTC (rev 9251)
+++
django/branches/releases/1.0.X/tests/regressiontests/model_inheritance_regress/models.py
2008-10-24 06:11:37 UTC (rev 9252)
@@ -257,4 +257,14 @@
# without error.
>>> _ = QualityControl.objects.create(headline="Problems in Django",
>>> pub_date=datetime.datetime.now(), quality=10, assignee="adrian")
+# Ordering should not include any database column more than once (this is most
+# likely to ocurr naturally with model inheritance, so we check it here).
+# Regression test for #9390. This necessarily pokes at the SQL string for the
+# query, since the duplicate problems are only apparent at that late stage.
+>>> sql = ArticleWithAuthor.objects.order_by('pub_date',
'pk').query.as_sql()[0]
+>>> fragment = sql[sql.find('ORDER BY'):]
+>>> pos = fragment.find('pub_date')
+>>> fragment.find('pub_date', pos + 1) == -1
+True
+
"""}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---