from sqlalchemy import event
from sqlalchemy.pool import Pool

@event.listens_for(Pool, 'checkout')
def on_pool_checkout(dbapi_conn, connection_rec, connection_proxy):
    tenant_id = session.get('tenant_id')
cursor = dbapi_conn.cursor()
if tenant_id is None:
    cursor.execute('SET search_path TO public, shared;')
else:
    cursor.execute('SET search_path TO t' + str(tenant_id) + ', shared;')
dbapi_conn.commit()
cursor.close()
Мультитенантность с SQLAlchemy