2014-03-02 18 views

回答

3

是的,你可以使用列表達式sqlalchemy.and_sqlalchemy.or_(注意最後的下劃線,而不是開始,領先的下劃線表示不支持,私有API)。

您還可以使用&|,位運算符,他們會以同樣的方式爲and_/or_進行編譯。一定要將它們加括號,&|andor具有更高的運算符優先級。

您的代碼看起來有點像:

t.select().where((t.c.id == 1) 
       & ((t.c.last_date == None) 
        | (t.c.last_date < sa.func.date('now', '-30 day')))) 

另一種選擇是把合取子句爲逐步過濾關係,因爲身份:

σ 一個∧ b [R ↔ σ b σ a R

你可以寫在SQLAlchemy的爲

t.select().where(t.c.id == 1) \ 
      .where((t.c.last_date == None) 
       | (t.c.last_date < sa.func.date('now', '-30 day'))) 

並可以產生相同的SQL如上。