1.4 to 2.0 Migration
SQLAlchemy 2.0, released in January 2023, adopts a unified execution style. Queries move from session.query() to session.execute(select(...)). Version 1.4 served as a migration bridge supporting both styles.
Before / After
python
from sqlalchemy import select, create_engine
from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = 'users'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
# SQLAlchemy 1.x: session.query(User).filter_by(name='Alice').first()
# SQLAlchemy 2.0:
engine = create_engine('sqlite:///db.sqlite3')
with Session(engine) as session:
stmt = select(User).where(User.name == 'Alice')
user = session.execute(stmt).scalar_one_or_none()
print(user)
