2

I'm just learning SQL Alchemy and using the ORM to pull in records.

However, there are some functions that appear to be unique to the other way of pulling in records, like sqlalchemy.and_',sqlalchemy.or_', etc.

I'm wondering how I can use the ORM but still employ some "OR" subclauses so I can do things like: given an ORM object Person select all records where last name is "Smith" or "Lee" or "Bishop" but exclude records where age is less than 18.

Is this only possible if I don't use the ORM?

4

1 回答 1

5

By default, filter clauses are appended with AND. So the code below will do it:

from sqlalchemy import or_

qry = (session.query(Person).
    filter(or_(Person.LastName == 'Smith', Person.LastName == 'Lee', Person.LastName == 'Bishop')).
    filter(Person.age >= 18)
    )
于 2013-04-30T07:59:35.053 回答