[sqlalchemy] Re: Ordering results of a WHERE x in y query by y

2009-02-26 Thread Ants Aasma
import sqlalchemy def index_in(col, valuelist): return sqlalchemy.case([(value,idx) for idx,value in enumerate (valuelist)], value=col) session.query(C).filter(C.someattr.in_(valuelist)).order_by(index_in (C.someattr, valuelist)) Don't try to do this with huge lists of items. On Feb 25,

[sqlalchemy] Re: Ordering results of a WHERE x in y query by y

2009-02-26 Thread Gunnlaugur Thor Briem
Thanks. But using a CASE clause becomes objectionable in exactly those cases where I would want to have the DB do the sorting — i.e. where the table is big enough that just sorting the result set in python code using array index (rows.sort(key=lambda row: values.index(row[0]))) would be a Bad