__tablename__ = 'blogs' id = sa.Column(sa.Integer, primary_key=True) # lazy = True / 'joined' are the same comments = sa.orm.relationship(lambda: Comment, lazy='dynamic', backref='blog') class Comment(Base, MethodsMixin): __tablename__ = 'comments' id = sa.Column(sa.Integer, primary_key=True) blog_id = sa.Column(sa.ForeignKey(Blog.id, ondelete='cascade'), nullable=False) init_entities(Blog, Comment) enable_logging() sess = Session() profiler = sqltap.ProfilingSession() with profiler: for b in sess.query(Blog): assert b.comments.order_by('id').all() query_stats = profiler.collect() assert len(query_stats) == 3
from dateutil import parser from sqlalchemy import inspect from models import Zebra from utils import step, configure_logger, Session, silence configure_logger() with step(): session = Session() # Grab any ol' Zebra zebra = session.query(Zebra).first() session.commit() with step(): print(f'Hello, {zebra.name}!') # Don't show this with silence(): session.close() # JUST FOR MY OWN VERIFICATION: What happens when you rollback? with step(): session = Session() # Grab any ol' Zebra print(session.query(Zebra).first()) # Rollback instead of commit session.rollback() print(f'Hello, {zebra.name}!') with step():