Build SQLAlchemy statements avoiding common footguns.
Check out the documentation
Build select statements functionally using | and |=
select(Model).where(Model.id == 1)Becomes:
from sqlalchemy_builder.select import where
select(Model) | where(Model.id == 1)|= is also supported:
statement = select(Model)
if value:
statement |= where(Model.value == value)$ uv add sqlalchemy-builderor
$ pip install sqlalchemy-builderThere is a common mistake when building sqlalchemy statements.
statement = select(Model)
if status is not None:
statement.where(Model.status == status)The statement.where does not do anything as it generates a new Select object.
To fix this we have to assign it:
statement = statement.where(...)This can be a bit verbose though not the worst issue. By using implace |= we can avoid this issue without making the statement mutable.
It's also just a fun syntax to play with.
Installing dependencies:
$ uv sync Running tests:
$ uv run pytest