0Pricing
Python Academy · Lesson

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

  1. SQLAlchemy Core vs ORM
  2. Defining Models
  3. Querying with the Session
  4. Relationships and Joins
← Back to Python Academy