如何在SQLAlchemy中嵌套连词或和_

时间:2014-10-02 03:15:49

标签: python sql sqlalchemy flask-sqlalchemy

我想在SQL Alchamey中重新创建此查询,但我无法嵌套过滤器:

查询:

SELECT * FROM calendar 
where (recurrenceRule = '') 
or (recurrenceRule != '' and start < @end1);

的Python:

events.filter(or_(Calendar.recurrenceRule!='',
      (Calendar.recurrenceRule=='',Calendar.start>=filterStart))

此python导致以下异常: &#34;预期的SQL表达式对象或字符串。&#34;

1 个答案:

答案 0 :(得分:2)

您必须明确使用and_

events.filter(or_(
    Calendar.recurrenceRule!='',
    and_(Calendar.recurrenceRule=='', Calendar.start>=filterStart))
)