SQL Alchemy
pip install sqlalchemy
# For PostgreSQL
pip install psycopg2
# For MySQL
pip install pymysqlUsage
Querying
from sqlalchemy import select
user = db.session.scalars(select(User).where(User.email == email)).first()
# query with multiple classes, returns list of tuples
result = session.execute(select(User, Address).join("addresses").filter_by(name="ed")).all()
# query with ORM columns, returns list of tuples
result = session.execute(select(User.name, User.fullname)).all()
# Joins, infers the on statement
users = db.session.scalars(select(User).join(Cohort).join(School).where(School.id == school_id)).all()Insert
Models
Relationships
Migrating/Creating Tables
Outside of Flask
Session
Last updated