SqlAlchemy or_ with multi-part clauses
<p>I have a list of independent clauses that look like:</p>

<pre><code>'cls.status = :status_1 AND cls.password_valid = 0'
'cls.status = :status_1 AND cls.reason = :reason_1'
...
</code></pre>

<p>I'm wanting to add them to the where clause in sqlalchemy</p>

<p>using <code>query = query.filter(or_(*clauses))</code> which says to me I should read them (in the where clause) as:</p>

<pre><code>WHERE ...
AND ((tbl1.status = :status_2 AND tbl1.password_valid = 0)
     OR (tbl1.status = :status_3 AND tbl1.reason = :reason_1))
</code></pre>

<p>For some reason when I add multipart clauses to the query, it sees them as single part and I get this instead:</p>

<pre><code>WHERE ...
AND (tbl1.status = :status_2 AND tbl1.password_valid = 0 
     OR tbl1.status = :status_3 AND tbl1.reason = :reason_1)
</code></pre>

<p>Which is NOT equivalent to what I expect.  How do I get it to introduce parens around the multipart clauses that are being OR'd together?  All the examples I find are single clause statements being OR'd so of course they work</p>

<p>Using <code>or_</code>, you have to wrap stuff up in a parens separated by commas.</p>

<p><code>query.filter(or_(
                  something == 'this', something == 'that'
                   )
               )
</code></p>

<p>If you want to separate your clauses with parens, you can use the alternate bitwise operator <code>|</code></p>

<p>ex:</p>

<p><code>query.filter( (something == 'this') |
                (something == 'that'))
</code></p>

python sqlalchemy