﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9406	Query.as_sql() generates invalid ORDER BY clause	egenix_viktor	Malcolm Tredinnick	"Backend: SQLite

Test case: `regressiontests/model_inheritance_regress`

Statement: `c1.get_next_by_pub_date()`

The `Query.as_sql()` method in `django.db.models.sql.query` generates two ordering items for the same `""model_inheritance_regress_article"".""pub_date""` field:

(The generated query has been split into multiple lines for readability.)

{{{
SELECT
    ""model_inheritance_regress_article"".""id"",
    ""model_inheritance_regress_article"".""headline"",
    ""model_inheritance_regress_article"".""pub_date"",
    ""model_inheritance_regress_articlewithauthor"".""article_ptr_id"",
    ""model_inheritance_regress_articlewithauthor"".""author""
FROM ""model_inheritance_regress_articlewithauthor""
INNER JOIN ""model_inheritance_regress_article""
ON (""model_inheritance_regress_articlewithauthor"".""article_ptr_id"" = ""model_inheritance_regress_article"".""id"")
WHERE (
    ""model_inheritance_regress_article"".""pub_date"" > %s OR  (
        ""model_inheritance_regress_articlewithauthor"".""article_ptr_id"" > %s AND
        ""model_inheritance_regress_article"".""pub_date"" = %s ))
ORDER BY
    ""model_inheritance_regress_article"".""pub_date"" ASC,
    ""model_inheritance_regress_article"".""pub_date"" DESC,
    ""model_inheritance_regress_article"".""headline"" ASC
LIMIT 1
}}}

Parameters:

{{{
(u'2005-08-01 03:00:00', 1, u'2005-08-01 03:00:00')
}}}

It works on SQLite for some reason, but such an `ORDER BY` clause causes an error message on many database servers, such as on MS SQL Server 2005 (when used with another backend, certainly)."		closed	Database layer (models, ORM)	1.0		fixed	Query as_sql ORDER clause ordering duplicate wrong SQL generate generates		Unreviewed	0	0	0	0	0	0
