Querying with the Session
Read and filter data.
The Session
The ORM does all its work through a Session. It manages a unit of work: tracking new, changed, and deleted objects, and talking to the database.
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
engine = create_engine('sqlite:///:memory:')
with Session(engine) as session:
print('Session open:', session)Adding Objects
session.add() stages a new object. Nothing hits the database until you commit().
from sqlalchemy.orm import Session
# session.add(user) stages it
# session.commit() writes it
print('add stages, commit saves')All lessons in this course
- SQLAlchemy Core vs ORM
- Defining Models
- Querying with the Session
- Relationships and Joins