Python

Python

Made by DeepSource

Logical operators used when combining SQL column expressions PY-W0800

Bug risk
Major
Autofix

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 `&`.
)