On Wed, Feb 17, 2016 at 7:46 AM, Krishnakant <krm...@openmailbox.org> wrote:

> Hello all,
> The subject might have made my problem already clear.
> So I am unclear about when I should use the normal Python "and " vs the
> sqlalchemy "and_" while writing where, having or similar queries including
> joins.
> I have tryed understanding this but may be I have overlooked some thing.
> Kindly give some pointers.
> Happy hacking.
> Krishnakant.
>
>
You probably don't ever want the python keyword "and" when constructing
queries. It doesn't trigger any of SQLAlchemy's smart SQL-construction
behaviour. You might be thinking of the bitwise operator "&", which
SQLAlchemy hijacks and converts to an SQL "AND".

As far as I'm aware, "expr1 & expr2" is completely equivalent to
"and_(expr1, expr2)" if expr1 and expr2 are both SQLAlchemy expressions, so
you can use whichever you prefer.

http://docs.sqlalchemy.org/en/rel_1_0/core/sqlelement.html#sqlalchemy.sql.expression.and_

Note the warning on that page about operator precedence though; the bitwise
operators have higher precedence than the comparison operators, so this
probably wouldn't do what you expect:

    expr1 == expr2 & expr3 == expr4

You'd need to write that as:

    (expr1 == expr2) & (expr3 == expr4)

Simon

-- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to