Notes Upgrade SQLAlchemy2.0
Wild collection of notes and changes while upgrading from SQLAlchemy1.4 to SQLAlchemy2.0 Before indicates the style for version 1.4, after indicates 2.0.
This post will get updated when I encounter more changes in my code.
**note on running pytest with warnings enabled: to run a pytest testsuite with all warnings enabled, instead of running pytest
, run this: SQLALCHEMY_WARN_20=1 python -W always -m pytest
1
declarative_base
import
Before:
from sqlalchemy.ext.declarative import declarative_base
After:
from sqlalchemy.orm import declarative_base
- Querying a table by a primary key/id:
Before:
obj = db.query(MyTable).get(id)
After:
obj = db.get(MyTable, id)
Source: https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session.get