On 02/21/2013 07:31 PM, Jonathan Vanasco wrote:
> basd on a bunch of error messages, this example works...
>
>     criteria = ( ('male',35),('female','35) )
>     query = session.query( model.Useraccount )
>     ands = []
>     for set_ in criteria :
>               ands.append(\
>                       sqlalchemy.sql.expression.and_(\
>                               model.Useraccoun.gender == set_[0] ,
>                               model.Useraccoun.age == set_[1] ,
>                       )
>               )
>       query = query.filter(\
>               sqlalchemy.sql.expression.or_( *ands )
>       )
>       results= query.all()
>
> this seems really awkward though.  is there a better way to build up a
> set of dynamic "or" criteria ?
>
For this specific case, if your database supports it, you can use the
tuple_ construct:

criteria = (('male', 35), ('female', 35))
query = session.query(model.Useraccount)
query = query.filter(sa.tuple_(model.Useraccount.gender, 
model.Useraccount.age).in_(criteria))
results = query.all()

It's cleaner and should give better index usage.

-Conor

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to