pythonsqlpostgresqlsqlalchemy

SQLAlchemy and partitioning table


I have tables which are already in Postgres and partitioned. How can I write a query with partioned birth_year

stmt = select(UserOrm)
session.execute(stmt)`

Solution

  • When using partitioned tables in PostgreSQL, SQLAlchemy does not require any special syntax. You query them the same way you would query a regular table. PostgreSQL handles the partition pruning automatically.

    stmt = select(UserOrm).where(UserOrm.birth_year == 1990)
    result = session.execute(stmt).scalars().all()