Re: [sqlalchemy] Where to use and vs and_()
On Wednesday, February 17, 2016 at 4:34:35 AM UTC-5, Simon King wrote: > > > (expr1 == expr2) & (expr3 == expr4) > you usually won't need to & or and_ though. filter automatically "and"s a list. .filter(expr1 == expr2, expr3 == expr4) the only you need to use `and_` is when doing more complicated queries and the "and" is nested. .filter(or_(expr1 == expr2, and_(expr2 == expr3, expr3 == expr4 )) -- 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.
Re: [sqlalchemy] Where to use and vs and_()
On Wed, Feb 17, 2016 at 7:46 AM, Krishnakantwrote: > 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.
[sqlalchemy] Where to use and vs and_()
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 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.