Logical operators used when combining SQL column expressions
99
100def get_active_users():
101 global user
102 sqlalchemy.select([user.id, user.name]).where(103 (user.org == "DeepSource") and (user.active == True)
104 )
105
Description
Column expressions inside a sqlalchemy where
or having
clause must be combined using the bitwise &
and |
operators, not the logical and
/ or
. Using logical operators cause various expressions to be ignored, leading to unintended behaviour.
You can read about the ways to combine SQL expressions in sqlalchemy here.
Bad practice
from sqlalchemy import select
select([user.id, user.name]).where(
(user.org == my_org) and (user.active == True) # Using `and` here won't work as you expect.
)
Recommended
from sqlalchemy import select
select([user.id, user.name]).where(
(user.org == my_org) & (user.active == True) # Replaced `and` with bitwise `&`.
)