SQLAlchemy Cheatsheet 05 — Relationships and Loading Strategies
Cheatsheet: relationship(), back_populates, secondary, cascade, joinedload / selectinload / subqueryload, lazy options.
Cheatsheet: relationship(), back_populates, secondary, cascade, joinedload / selectinload / subqueryload, lazy options.
Postgres locking patterns: SELECT FOR UPDATE, NOWAIT, SKIP LOCKED, advisory locks, application locks, deadlock retry.
Selecting rows in SQLAlchemy 2.0: select(), where, joins, group_by, aggregates, subqueries, CTEs, scalars vs rows.
Cheatsheet: tenant context, RLS policies, per-request session, isolation tests.
Cheatsheet: generated tsvector, websearch_to_tsquery, ts_rank, headline snippets, trigram hybrid.
Cheatsheet: session.begin, begin_nested, isolation levels, with_for_update, optimistic locking, deadlock retry.
LISTEN/NOTIFY patterns: real-time event delivery, queue wake-up, payload limits, and combining with the outbox pattern.
Relationship() in depth: configuration, loading strategies, N+1 prevention, association tables, polymorphic patterns.
Cheatsheet: row locking, NOWAIT, SKIP LOCKED queue, advisory locks (session and tx scoped).
Cheatsheet: AsyncEngine, AsyncSession, lazy loading caveats, streaming results, run_sync escape hatch.