Query result ordering on field in different table broken when doing join on same table
|Reported by:||rfugger at gmail dot com||Owned by:||adrian|
|Component:||Database layer (models, ORM)||Version:||0.91|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
(Using django 0.91/postgresql)
Doing a join on table X creates an alias like "t1" for table X in the SQL. If I then order_by='X.something', I get the following undesirable behaviour:
"The alias becomes the new name of the table reference for the current query — it is no longer possible to refer to the table by the original name. Thus
SELECT * FROM my_table AS m WHERE my_table.a > 5;
is not valid SQL syntax. What will actually happen (this is a PostgreSQL extension to the standard) is that an implicit table reference is added to the FROM clause, so the query is processed as if it were written as
SELECT * FROM my_table AS m, my_table AS my_table WHERE my_table.a > 5;
which will result in a cross join, which is usually not what you want."
Django must rename my order_by argument to account for the alias it has given the table. It doesn't appear to be doing this.