#2076: order_by with related table does not work
---------------------------+------------------------------------------------
Reporter: mtredinnick | Owner: mtredinnick
Status: new | Component: Database wrapper
Version: SVN | Resolution:
Keywords: | Stage: Accepted
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 1
---------------------------+------------------------------------------------
Comment (by Michael Radziej <[EMAIL PROTECTED]>):
The supplied testcases do not work with a postgresql database, since it
requires that there is an order-by clause that matches the distinct
clause. I get these error messages:
{{{
======================================================================
FAIL: Doctest: modeltests.ordering.models.__test__.API_TESTS
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mir/lib/python/django/test/doctest.py", line 2156, in
runTest
raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for
modeltests.ordering.models.__test__.API_TESTS
File "/home/mir/src/django/trunk/tests/modeltests/ordering/models.py",
line unknown line number, in API_TESTS
----------------------------------------------------------------------
File "/home/mir/src/django/trunk/tests/modeltests/ordering/models.py",
line ?, in modeltests.ordering.models.__test__.API_TESTS
Failed example:
Reporter.objects.all().distinct()
Exception raised:
Traceback (most recent call last):
File "/home/mir/lib/python/django/test/doctest.py", line 1243, in
__run
compileflags, 1) in test.globs
File "<doctest modeltests.ordering.models.__test__.API_TESTS[38]>",
line 1, in ?
Reporter.objects.all().distinct()
File "/home/mir/lib/python/django/db/models/query.py", line 104, in
__repr__
return repr(self._get_data())
File "/home/mir/lib/python/django/db/models/query.py", line 505, in
_get_data
self._result_cache = list(self.iterator())
File "/home/mir/lib/python/django/db/models/query.py", line 189, in
iterator
cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "")
+ ",".join(select) + sql, params)
File "/home/mir/lib/python/django/db/backends/postgresql/base.py",
line 43, in execute
return self.cursor.execute(sql, [smart_basestring(p, self.charset)
for p in params])
ProgrammingError: ERROR: for SELECT DISTINCT, ORDER BY expressions
must appear in select list
SELECT DISTINCT "ordering_reporter"."id","ordering_reporter"."name"
FROM "ordering_reporter" LEFT OUTER JOIN "ordering_reporter_artic
les" AS "m2m_ordering_reporter__articles" ON "ordering_reporter"."id" =
"m2m_ordering_reporter__articles"."reporter_id" INNER JOIN "order
ing_article" AS "ordering_reporter__articles" ON
"m2m_ordering_reporter__articles"."article_id" =
"ordering_reporter__articles"."id" ORDE
R BY "ordering_reporter__articles"."headline" ASC
}}}
(and more for the other distinct queries). Unfortunately, the tests cannot
be easily rephrased, since they really require {{{distinct}}}.
The [http://www.postgresql.org/docs/8.1/interactive/sql-select.html#SQL-
DISTINCT postgresql docs] state:
''The DISTINCT ON expression(s) must match the leftmost ORDER BY
expression(s).''
Though, the patch works great for me ;-)
--
Ticket URL: <http://code.djangoproject.com/ticket/2076#comment:28>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---