6 messages in com.googlegroups.sqlalchemy[sqlalchemy] sorting mapped class by ...
FromSent OnAttachments
Dennis31 Jan 2007 10:11 
Michael Bayer31 Jan 2007 11:26 
Dennis31 Jan 2007 11:55 
Dennis31 Jan 2007 12:06 
Dennis31 Jan 2007 13:57 
Michael Bayer31 Jan 2007 15:46 
Subject:[sqlalchemy] sorting mapped class by joined subquery result (error)
From:Dennis (djmu@gmail.com)
Date:01/31/2007 10:11:51 AM
List:com.googlegroups.sqlalchemy

I have a mapped class.. lets call it Data with a few properties

Data.id (primary key), Data.a, Data.b, Data.c

I want to query a few of these objects out.. but they need to be sorted by some arbitrary data

arbitrary_data=select ( [Data.c.id, OtherClass.c.somedata], and_(....)).alias('somedata')

ok.. now query the data:

dat=Data.select( and_(.....), from_obj=[ datas.join(arbitrary_data,arbitrary_data.c.id==datas.c.id) ] , order_by=[asc(arbitrary_data.c.somedata)])

Now, the generated sql is in the form (with query.py deciding it needs to nest the query):

select datas.id as datas_id, datas.a as datas_a .... etc. from (select datas.id as datas_id, arbitrary_data.somedata as arbitrary_data_somedata from datas join (my arbitrary_data table query with where clause ) as arbitrary_data where ..... order by arbitrary_data.somedata ) as tbl_row_count join datas on ... order by arbitrary_data.somedata

The last line is the problem.. The from clause renames the column to arbitrary_data_somedata but the order by clause uses the inner form with a . still.

The error: missing FROM-clause entry for table "arbitrary_data" (because that table only exists on the inner aliased table)

Anyhow, if I rename the sort on the outer query to use the underscore manually, the query returns the correct results in the correct order.

I believe the faulty behavior starts at line 455 in orm/query.py (trunk). I'm not sure if it is the Aliasizer that is not converting the column. Anyhow, I need this to work so I don't have to write my great big huge dynamic query out by hand so I'll be digging into the sqlalchemy code for a second.

Is there is quick easy fix though?