Package sqlalchemy_builder

Build SQLAlchemy statements avoiding common footguns.

Example

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)

Why?

There 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.

Sub-modules

sqlalchemy_builder.select

Sqlalchemy Select methods redefined statically …