SQLAlchemy Core, ORM & sqlalchemy_utils — Interview Preparation
Total Questions: 300 Levels: Core Fundamentals (100), Intermediate (120), Advanced (80)
Table of Contents
1. Core Fundamentals
Q1: What is SQLAlchemy and what are its two main components?
Technical Answer: SQLAlchemy is a Python SQL toolkit and ORM. It has two distinct layers: SQLAlchemy Core (a schema-centric SQL expression language that lets you write SQL in Python) and SQLAlchemy ORM (a higher-level object-relational mapper built on top of Core that maps Python classes to database tables).
Code Example:
# Core style — SQL expressions
from sqlalchemy import create_engine, text
engine = create_engine("postgresql+psycopg2://user:pass@localhost/mydb")
with engine.connect() as conn:
result = conn.execute(text("SELECT * FROM users WHERE id = :id"), {"id": 1})
# ORM style — Python objects
from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
with Session(engine) as session:
user = session.get(User, 1)
🔍 Interviewer Notes: Tests foundational understanding. Candidates should know Core vs ORM is not either/or — ORM uses Core underneath.
❌ Common Mistakes:
- Thinking ORM and Core are competing alternatives rather than layered
- Using
engine.execute()which was removed in SQLAlchemy 2.0 - Confusing
Session(ORM) withConnection(Core)
Q2: How do you create an Engine and what does it represent?
Technical Answer:
An Engine is the starting point for any SQLAlchemy application. It represents a connection pool plus a dialect (database-specific SQL translator). create_engine() is lazy — it does not open connections until you first use it.
Code Example:
from sqlalchemy import create_engine
# PostgreSQL
engine = create_engine(
"postgresql+psycopg2://user:password@localhost:5432/dbname",
pool_size=10,
max_overflow=20,
pool_pre_ping=True, # health-check connections before use
echo=False, # set True to log all SQL
)
# SQLite (in-memory, useful for tests)
test_engine = create_engine("sqlite:///:memory:")
# Verify connection
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
🔍 Interviewer Notes: Tests knowledge of connection pooling configuration and lazy initialization.
❌ Common Mistakes:
- Calling
create_engine()per request (should be created once at app startup) - Forgetting
pool_pre_ping=Truein long-running services (stale connections) - Hardcoding credentials in the connection string instead of using env vars
Q3: What is the difference between Session and Connection?
Technical Answer:
Connection is a Core concept — a single DBAPI connection from the pool used to execute SQL statements. Session is an ORM concept — a unit-of-work container that tracks Python objects, manages the identity map, and batches changes before flushing them to the database via an underlying Connection.
Code Example:
from sqlalchemy import create_engine, text
from sqlalchemy.orm import Session
engine = create_engine("sqlite:///:memory:")
# Core: raw Connection
with engine.connect() as conn:
conn.execute(text("CREATE TABLE t (id INTEGER PRIMARY KEY)"))
conn.commit()
# ORM: Session manages object state
with Session(engine) as session:
user = User(name="Alice")
session.add(user)
session.commit() # flushes and commits
🔍 Interviewer Notes: Critical distinction for debugging. Knowing when each is appropriate shows architectural maturity.
❌ Common Mistakes:
- Using
Sessionfor raw SQL whenConnectionis simpler and faster - Forgetting that a
Sessiondoes not hold aConnectionopen between flushes (in 2.0) - Mixing Core and ORM patterns on the same object inconsistently
Q4: What is the SQLAlchemy Session lifecycle and what are the four object states?
Technical Answer:
A SQLAlchemy ORM object can be in one of four states: Transient (created but not added to session), Pending (added with session.add() but not yet flushed), Persistent (present in session and has a database identity), Detached (had a database identity but its session was closed or it was expunged).
Code Example:
from sqlalchemy.orm import Session
from sqlalchemy import inspect as sa_inspect
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
user = User(name="Bob") # Transient
print(sa_inspect(user).transient) # True
with Session(engine) as session:
session.add(user)
print(sa_inspect(user).pending) # True — not yet flushed
session.flush()
print(sa_inspect(user).persistent) # True — has identity
# After session closes
print(sa_inspect(user).detached) # True
🔍 Interviewer Notes: Detached-state errors (DetachedInstanceError) are common bugs in production. Understanding states prevents them.
❌ Common Mistakes:
- Accessing lazy-loaded relationships on detached objects
- Not understanding that
session.commit()expires all persistent objects - Confusing
session.expunge()(detach) withsession.delete()(mark for deletion)
Q5: How do you define a model using the modern DeclarativeBase approach (SQLAlchemy 2.x)?
Technical Answer:
In SQLAlchemy 2.0+, the recommended approach is to subclass DeclarativeBase and use Mapped[type] annotations with mapped_column(). This provides full type-checking support and is more Pythonic than the legacy Column() approach.
Code Example:
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy import String, Integer, ForeignKey
from datetime import datetime
from typing import Optional, List
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100), nullable=False)
email: Mapped[str] = mapped_column(String(255), unique=True)
age: Mapped[Optional[int]] = mapped_column(Integer, default=None)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
posts: Mapped[List["Post"]] = relationship(back_populates="author")
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
author: Mapped["User"] = relationship(back_populates="posts")
🔍 Interviewer Notes: Distinguishes candidates who know modern SQLAlchemy 2.x from those still using legacy 1.x patterns.
❌ Common Mistakes:
- Using legacy
Column()syntax withoutmapped_column()wrapper - Forgetting
Optionalfor nullable columns inMappedannotations - Defining
Base = declarative_base()instead ofclass Base(DeclarativeBase): pass
Q6: What SQLAlchemy column types map to common Python/database types?
Technical Answer: SQLAlchemy provides generic types (portable across databases) and dialect-specific types. Generic types are preferred for portability; dialect-specific types are used when you need DB-specific features.
Code Example:
from sqlalchemy import (
Integer, BigInteger, SmallInteger,
String, Text, Unicode,
Float, Numeric, Double,
Boolean,
Date, DateTime, Time, Interval,
LargeBinary, JSON, Enum,
Uuid, # SQLAlchemy 2.0+
)
from sqlalchemy.dialects.postgresql import JSONB, ARRAY, UUID as PG_UUID
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(200)) # VARCHAR(200)
description: Mapped[str] = mapped_column(Text) # TEXT
price: Mapped[float] = mapped_column(Numeric(10, 2)) # DECIMAL(10,2)
in_stock: Mapped[bool] = mapped_column(Boolean)
tags: Mapped[list] = mapped_column(ARRAY(String)) # PG only
meta: Mapped[dict] = mapped_column(JSONB) # PG JSONB
uid: Mapped[str] = mapped_column(PG_UUID(as_uuid=True))
🔍 Interviewer Notes: Tests breadth of SQLAlchemy type knowledge and awareness of portability vs. performance tradeoffs.
❌ Common Mistakes:
- Using
Floatfor money (precision loss) — useNumeric(precision, scale) - Using
JSONwhenJSONBis available in PostgreSQL (JSONB is indexable) - Ignoring
Stringlength limit which may silently truncate on some DBs
Q7: How do you perform basic CRUD operations with the ORM?
Technical Answer:
Create uses session.add(), Read uses session.get() or select(), Update modifies object attributes directly, Delete uses session.delete(). All changes are committed with session.commit().
Code Example:
from sqlalchemy.orm import Session
from sqlalchemy import select
with Session(engine) as session:
# CREATE
user = User(name="Alice", email="alice@example.com")
session.add(user)
session.commit()
session.refresh(user) # reload generated fields (id, created_at)
print(user.id) # now populated
# READ by PK
user = session.get(User, 1)
# READ with query
stmt = select(User).where(User.name == "Alice")
user = session.scalars(stmt).first()
# UPDATE
user.name = "Alice Smith"
session.commit() # SQLAlchemy detects the change automatically
# DELETE
session.delete(user)
session.commit()
🔍 Interviewer Notes: Covers the full ORM lifecycle. Look for knowledge of session.refresh() and understanding of automatic change detection.
❌ Common Mistakes:
- Calling
session.add()for updates (only needed for new objects) - Not calling
session.refresh()after commit when you need generated values - Using
query()API (deprecated in 2.0) instead ofselect()
Q8: How do you filter queries using SQLAlchemy ORM?
Technical Answer:
Use select() with .where() for single conditions, or combine with and_(), or_(), not_() for compound conditions. Column comparison operators work natively on mapped columns.
Code Example:
from sqlalchemy import select, and_, or_, not_
with Session(engine) as session:
# Simple filter
stmt = select(User).where(User.age > 18)
# Multiple conditions (implicit AND)
stmt = select(User).where(User.age > 18, User.name.like("A%"))
# Explicit AND / OR
stmt = select(User).where(
and_(User.age > 18, User.age < 65)
)
stmt = select(User).where(
or_(User.name == "Alice", User.name == "Bob")
)
# IN clause
stmt = select(User).where(User.id.in_([1, 2, 3]))
# NULL check
stmt = select(User).where(User.email.is_(None))
stmt = select(User).where(User.email.is_not(None))
users = session.scalars(stmt).all()
🔍 Interviewer Notes: Tests knowledge of the expression API vs. legacy filter_by() (keyword-only, no operators).
❌ Common Mistakes:
- Using Python
==withNoneinstead of.is_(None)(generatesIS NULLcorrectly) - Forgetting that
filter_by()only supports equality; complex conditions needwhere() - Using
%wildcard inlike()without escaping user input
Q9: How do you order, limit, and paginate query results?
Technical Answer:
Use .order_by(), .limit(), and .offset() on a select() statement. For production pagination, keyset (cursor-based) pagination is preferred over offset for large datasets.
Code Example:
from sqlalchemy import select, desc, asc
with Session(engine) as session:
# Order by single column
stmt = select(User).order_by(User.created_at.desc())
# Multiple order columns
stmt = select(User).order_by(desc(User.age), asc(User.name))
# Limit and offset (page 2, 10 per page)
page, per_page = 2, 10
stmt = select(User).order_by(User.id).limit(per_page).offset((page - 1) * per_page)
users = session.scalars(stmt).all()
# Keyset pagination (efficient for large tables)
last_seen_id = 100
stmt = select(User).where(User.id > last_seen_id).order_by(User.id).limit(10)
users = session.scalars(stmt).all()
🔍 Interviewer Notes: Keyset vs offset pagination is a senior-level differentiation point.
❌ Common Mistakes:
- Using
OFFSETon large tables (full scan up to offset point) - Not including
ORDER BYwhen usingLIMIT/OFFSET(non-deterministic results) - Using
len(session.scalars(stmt).all())for counts instead offunc.count()
Q10: How do you count rows and aggregate data?
Technical Answer:
Use func.count(), func.sum(), func.avg(), func.max(), func.min() from sqlalchemy.sql. For ORM models, combine with select() and session.scalar() to return a single value.
Code Example:
from sqlalchemy import select, func
with Session(engine) as session:
# Count all
count = session.scalar(select(func.count()).select_from(User))
# Count with filter
adult_count = session.scalar(
select(func.count(User.id)).where(User.age >= 18)
)
# Aggregates
result = session.execute(
select(
func.avg(User.age).label("avg_age"),
func.max(User.age).label("max_age"),
func.min(User.age).label("min_age"),
)
).one()
# Group by
stmt = (
select(User.name, func.count(Post.id).label("post_count"))
.join(Post, Post.author_id == User.id)
.group_by(User.name)
.having(func.count(Post.id) > 5)
.order_by(func.count(Post.id).desc())
)
rows = session.execute(stmt).all()
🔍 Interviewer Notes: Tests SQL expression knowledge. session.scalar() vs session.execute() distinction matters.
❌ Common Mistakes:
- Using
session.query(func.count(...))(legacy) instead ofselect(func.count(...)) - Forgetting
.label()on aggregates when accessing by attribute name - Not using
HAVINGfor post-aggregation filtering (usingWHEREinstead)
Q11: How do you define and use ForeignKey relationships?
Technical Answer:
ForeignKey links a column to another table's column. Combined with relationship(), it enables ORM-level navigation between objects. The back_populates parameter creates a bidirectional relationship.
Code Example:
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, Mapped, mapped_column
from typing import List, Optional
class Department(Base):
__tablename__ = "departments"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
employees: Mapped[List["Employee"]] = relationship(back_populates="department")
class Employee(Base):
__tablename__ = "employees"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
department_id: Mapped[Optional[int]] = mapped_column(ForeignKey("departments.id"))
department: Mapped[Optional["Department"]] = relationship(back_populates="employees")
# Usage
with Session(engine) as session:
dept = Department(name="Engineering")
emp = Employee(name="Alice", department=dept)
session.add(emp)
session.commit()
print(emp.department.name) # "Engineering"
print(dept.employees[0].name) # "Alice"
🔍 Interviewer Notes: Tests relationship configuration. Bidirectional setup with back_populates is the modern recommended approach over backref.
❌ Common Mistakes:
- Using string
"tablename.column"inForeignKeybut getting the table name wrong - Using
backref(sets up in one place) vsback_populates(explicit, preferred) - Not understanding that
relationship()is lazy-loaded by default
Q12: What is lazy loading vs eager loading in SQLAlchemy?
Technical Answer:
Lazy loading fetches related objects on first access (N+1 risk). Eager loading fetches them upfront using JOIN (joinedload) or a separate IN query (selectinload). The default is lazy loading (lazy="select").
Code Example:
from sqlalchemy.orm import selectinload, joinedload
with Session(engine) as session:
# Lazy loading — triggers extra query on access
users = session.scalars(select(User)).all()
for user in users:
print(user.posts) # N separate SQL queries!
# Eager loading with selectinload (separate IN query — best for collections)
stmt = select(User).options(selectinload(User.posts))
users = session.scalars(stmt).all()
for user in users:
print(user.posts) # no extra query
# Eager loading with joinedload (JOIN — best for many-to-one)
stmt = select(Post).options(joinedload(Post.author))
posts = session.scalars(stmt).unique().all()
for post in posts:
print(post.author.name) # no extra query
🔍 Interviewer Notes: N+1 problem awareness is a key signal for backend experience. selectinload vs joinedload choice matters for performance.
❌ Common Mistakes:
- Using
joinedloadon one-to-many (causes duplicate rows without.unique()) - Not using
.unique()afterjoinedloadon collections - Assuming
lazy="joined"on the model is always better (increases base query cost)
Q13: How do you perform JOIN queries with SQLAlchemy?
Technical Answer:
Use .join() for INNER JOIN and .outerjoin() for LEFT OUTER JOIN. In SQLAlchemy 2.0, joins are chained on select() statements. The ON condition is inferred from ForeignKey definitions or provided explicitly.
Code Example:
from sqlalchemy import select
with Session(engine) as session:
# INNER JOIN (inferred from ForeignKey)
stmt = (
select(User, Post)
.join(Post, Post.author_id == User.id)
.where(Post.published == True)
)
# LEFT OUTER JOIN
stmt = (
select(User)
.outerjoin(Post, Post.author_id == User.id)
.where(Post.id == None) # users with no posts
)
# Multiple joins
stmt = (
select(Employee)
.join(Department, Employee.department_id == Department.id)
.join(Location, Department.location_id == Location.id)
.where(Location.city == "NYC")
)
results = session.execute(stmt).all()
🔍 Interviewer Notes: Tests SQL fluency expressed through the ORM. Explicit ON condition vs inferred is an important distinction.
❌ Common Mistakes:
- Using
select(User, Post)but iterating results as if they'reUserobjects - Forgetting
.unique()when joins produce duplicate rows - Using implicit cross join by accident (missing
.join(), using.where()for join condition)
Q14: How do you define many-to-many relationships?
Technical Answer:
Many-to-many requires an association table. In SQLAlchemy, this is done with a Table object (for simple cases) or a full model class (when the association has extra columns).
Code Example:
from sqlalchemy import Table, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
# Simple association table (no extra columns)
post_tags = Table(
"post_tags",
Base.metadata,
Column("post_id", Integer, ForeignKey("posts.id"), primary_key=True),
Column("tag_id", Integer, ForeignKey("tags.id"), primary_key=True),
)
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
tags: Mapped[List["Tag"]] = relationship(secondary=post_tags, back_populates="posts")
class Tag(Base):
__tablename__ = "tags"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(50))
posts: Mapped[List["Post"]] = relationship(secondary=post_tags, back_populates="tags")
# Usage
with Session(engine) as session:
tag = Tag(name="python")
post = Post(title="SQLAlchemy Guide", tags=[tag])
session.add(post)
session.commit()
🔍 Interviewer Notes: Distinguishes candidates who understand when to use a plain Table vs a full association model.
❌ Common Mistakes:
- Defining the association table after the models that reference it (forward reference issues)
- Using an association model when the extra columns make a plain
Tableinsufficient - Not setting
cascadeoptions appropriately (orphaned records)
Q15: What is session.flush() vs session.commit()?
Technical Answer:
flush() writes pending changes to the database within the current transaction without committing — the changes are visible to subsequent queries in the same session but not to other sessions. commit() makes the transaction permanent and expires all loaded objects.
Code Example:
with Session(engine) as session:
user = User(name="Test")
session.add(user)
session.flush() # writes to DB, generates user.id
print(user.id) # available because flush sent INSERT
# Can still rollback
session.rollback() # undoes the INSERT
session.add(user)
session.commit() # permanent — user.id generated, objects expired
# After commit, accessing user.name triggers a lazy reload
print(user.name) # re-fetches from DB
🔍 Interviewer Notes: The flush/commit distinction explains why you can access generated PKs before commit. This is a common interview question for Django developers switching to SQLAlchemy.
❌ Common Mistakes:
- Calling
flush()thinking it commits (data not durable untilcommit()) - Forgetting that
commit()expires all objects (accessing attributes after causes a reload) - Disabling
autoflush=Falsewithout understanding the consequences
Q16: How does SQLAlchemy handle transactions?
Technical Answer:
Every Session operates within a transaction by default. The transaction begins on first database access and ends with commit() or rollback(). In Core, engine.begin() provides an explicit transaction context manager.
Code Example:
# ORM transaction
with Session(engine) as session:
try:
session.add(User(name="Alice"))
session.add(User(name="Bob"))
session.commit()
except Exception:
session.rollback()
raise
# Context manager auto-rollback on exception
with Session(engine) as session:
with session.begin(): # explicit transaction block
session.add(User(name="Charlie"))
# commits here if no exception, rolls back if exception
# Core transaction
with engine.begin() as conn: # auto-commit on exit, auto-rollback on exception
conn.execute(text("INSERT INTO users (name) VALUES ('Dave')"))
🔍 Interviewer Notes: Understanding autobegin (implicit transaction start) vs explicit begin() is important for production reliability.
❌ Common Mistakes:
- Not wrapping multi-step operations in a single transaction (partial writes)
- Using
session.execute()without a transaction for write operations - Swallowing exceptions without rollback (session in broken state)
Q17: How do you use SQLAlchemy's text() for raw SQL?
Technical Answer:
text() creates a textual SQL expression. Use bound parameters with :param_name syntax to safely inject values. Never use Python string formatting for user input — it causes SQL injection.
Code Example:
from sqlalchemy import text
with engine.connect() as conn:
# Safe parameterized query
result = conn.execute(
text("SELECT * FROM users WHERE age > :min_age AND name LIKE :pattern"),
{"min_age": 18, "pattern": "A%"}
)
rows = result.fetchall()
# DDL with text
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)"))
conn.commit()
# In ORM Session
with Session(engine) as session:
users = session.execute(
text("SELECT id, name FROM users WHERE department_id = :dept"),
{"dept": 5}
).fetchall()
🔍 Interviewer Notes: Tests SQL injection awareness. Parameterized queries are non-negotiable.
❌ Common Mistakes:
text(f"SELECT * FROM users WHERE id = {user_id}")— SQL injection vulnerability- Forgetting that
text()results areRowobjects, not ORM instances - Not calling
conn.commit()after DML when usingengine.connect()(useengine.begin()instead)
Q18: What is the SQLAlchemy inspect function and what can it do?
Technical Answer:
sqlalchemy.inspect() is a universal inspection function. When passed an Engine or Connection, it returns an Inspector that can query database schema metadata (tables, columns, indexes, constraints). When passed an ORM mapped object or class, it returns mapper or instance state.
Code Example:
from sqlalchemy import inspect, create_engine
engine = create_engine("postgresql+psycopg2://user:pass@localhost/mydb")
# Get Inspector from engine
insp = inspect(engine)
# List all table names in the default schema
tables = insp.get_table_names()
print(tables) # ['users', 'posts', 'orders']
# Get columns for a specific table
columns = insp.get_columns("users")
for col in columns:
print(col["name"], col["type"], col["nullable"])
# name: str, type: VARCHAR(100), nullable: False
# Inspect an ORM instance state
from sqlalchemy import inspect as sa_inspect
user = User(name="Alice")
state = sa_inspect(user)
print(state.transient) # True
print(state.attrs.name.value) # "Alice"
🔍 Interviewer Notes: inspect() is dual-purpose — schema inspection vs instance state. Many candidates only know one use.
❌ Common Mistakes:
- Importing
inspectfrom Python's standard library instead of SQLAlchemy - Calling
insp.get_columns("users")on a non-existent table (raisesNoSuchTableError) - Using reflection-based inspection in a hot path (it queries
information_schema— cache results)
Q19: How do you use inspect.get_columns() and what does it return?
Technical Answer:
Inspector.get_columns(table_name) returns a list of dicts, each describing one column: name, type, nullable, default, autoincrement, comment. It queries the database's schema metadata directly — useful for dynamic schema inspection.
Code Example:
from sqlalchemy import inspect, create_engine
engine = create_engine("postgresql+psycopg2://user:pass@localhost/mydb")
insp = inspect(engine)
columns = insp.get_columns("users")
for col in columns:
print(f"Column: {col['name']}")
print(f" Type: {col['type']}")
print(f" Nullable: {col['nullable']}")
print(f" Default: {col['default']}")
print(f" Primary Key: {col.get('primary_key', False)}")
print(f" Comment: {col.get('comment', '')}")
# Example output:
# Column: id
# Type: INTEGER
# Nullable: False
# Default: nextval('users_id_seq'::regclass)
# Primary Key: True
# With schema
columns = insp.get_columns("users", schema="public")
# Build a dynamic column map
col_map = {col["name"]: col["type"] for col in insp.get_columns("users")}
🔍 Interviewer Notes: Tests practical schema introspection ability. Common in migration tooling, dynamic query builders, and data validation.
❌ Common Mistakes:
- Assuming column order from
get_columns()matchesSELECT *order (usually true but not guaranteed) - Not passing
schema=parameter when tables are in non-default schemas - Treating the
typevalue as a string — it's a SQLAlchemyTypeEngineobject
Q20: How do you use inspect.get_table_names(), get_foreign_keys(), and get_indexes()?
Technical Answer:
These Inspector methods provide full database schema introspection. get_table_names() lists tables, get_foreign_keys() returns FK constraints with source/target columns, get_indexes() returns all indexes with their columns and uniqueness.
Code Example:
from sqlalchemy import inspect
insp = inspect(engine)
# All tables in default schema
tables = insp.get_table_names()
# ['users', 'posts', 'orders']
# All tables in a specific schema
pg_tables = insp.get_table_names(schema="analytics")
# Foreign keys on 'posts' table
fks = insp.get_foreign_keys("posts")
for fk in fks:
print(f"FK: {fk['constrained_columns']} -> {fk['referred_table']}.{fk['referred_columns']}")
# FK: ['author_id'] -> users.['id']
# Indexes on 'users' table
indexes = insp.get_indexes("users")
for idx in indexes:
print(f"Index: {idx['name']}, Columns: {idx['column_names']}, Unique: {idx['unique']}")
# Index: idx_users_email, Columns: ['email'], Unique: True
# Primary key constraint
pk = insp.get_pk_constraint("users")
print(pk)
# {'constrained_columns': ['id'], 'name': 'users_pkey'}
🔍 Interviewer Notes: Often used in migration validation, admin panels, and documentation generation.
❌ Common Mistakes:
- Not knowing that
get_foreign_keys()returns the FK from the child table perspective - Assuming indexes include the PK index (get PK separately with
get_pk_constraint()) - Caching
inspect()results when the schema may have changed at runtime
Q21: How do you reflect an existing database table into SQLAlchemy?
Technical Answer:
Reflection loads table structure from the database at runtime. You can reflect a single Table with autoload_with=engine, or reflect all tables at once via MetaData.reflect().
Code Example:
from sqlalchemy import MetaData, Table, create_engine
engine = create_engine("postgresql+psycopg2://user:pass@localhost/mydb")
# Reflect a single table
meta = MetaData()
users = Table("users", meta, autoload_with=engine)
print([c.name for c in users.columns]) # ['id', 'name', 'email', ...]
# Reflect ALL tables
meta.reflect(bind=engine)
for table_name, table in meta.tables.items():
print(table_name, [c.name for c in table.columns])
# Use reflected table with Core
from sqlalchemy import select
with engine.connect() as conn:
result = conn.execute(select(users).limit(10))
for row in result:
print(row._mapping)
🔍 Interviewer Notes: Reflection is used when you don't own the schema (legacy databases, third-party systems). Automap can go further by generating ORM classes.
❌ Common Mistakes:
- Reflecting tables at request time (expensive — do it once at startup)
- Not knowing about
automap_base()for full ORM reflection - Forgetting that reflected tables don't have ORM features (no
session.add())
Q22: What is MetaData and how is it used?
Technical Answer:
MetaData is a container that holds Table definitions. It's the bridge between Python schema definitions and the database. create_all() creates all tables; drop_all() drops them. Every Table is registered in a MetaData instance.
Code Example:
from sqlalchemy import MetaData, Table, Column, Integer, String, create_engine
engine = create_engine("sqlite:///:memory:")
meta = MetaData()
users = Table(
"users", meta,
Column("id", Integer, primary_key=True),
Column("name", String(100), nullable=False),
Column("email", String(255), unique=True),
)
# Create all tables defined in meta
meta.create_all(engine)
# Drop all tables
meta.drop_all(engine)
# In ORM, Base.metadata is the MetaData
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
# All ORM models subclassing Base register in Base.metadata
Base.metadata.create_all(engine)
Base.metadata.drop_all(engine)
# Inspect what's registered
print(Base.metadata.tables.keys())
🔍 Interviewer Notes: Understanding MetaData is essential for migration tooling and testing (creating/dropping test DBs).
❌ Common Mistakes:
- Creating multiple
MetaDatainstances for models that need to be in the samecreate_all()call - Calling
create_all()in production code path (should be in migrations only) - Not knowing that
checkfirst=True(default) skips existing tables
Q23: How do you use UniqueConstraint and CheckConstraint?
Technical Answer:
UniqueConstraint enforces uniqueness across multiple columns (composite unique). CheckConstraint enforces a SQL expression at the database level. Both can be defined inline on a column or in __table_args__.
Code Example:
from sqlalchemy import UniqueConstraint, CheckConstraint, Index
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class Employee(Base):
__tablename__ = "employees"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String(255))
department: Mapped[str] = mapped_column(String(100))
salary: Mapped[float] = mapped_column()
status: Mapped[str] = mapped_column(String(20))
__table_args__ = (
# Composite unique constraint
UniqueConstraint("email", "department", name="uq_emp_email_dept"),
# Check constraint
CheckConstraint("salary > 0", name="chk_positive_salary"),
CheckConstraint("status IN ('active', 'inactive', 'suspended')", name="chk_status"),
# Partial index (PostgreSQL)
Index("idx_active_employees", "department", postgresql_where="status = 'active'"),
)
🔍 Interviewer Notes: Tests schema design knowledge. __table_args__ as a tuple (not dict) when mixing constraints and other args is a common gotcha.
❌ Common Mistakes:
- Using only a single
UniqueConstraintin__table_args__as a dict instead of a tuple - Forgetting that
CheckConstraintis not enforced by all databases at the application level - Not naming constraints (anonymous constraints are hard to reference in migrations)
Q24: How do you add indexes in SQLAlchemy?
Technical Answer:
Indexes can be defined with index=True on mapped_column() for simple cases, or with an explicit Index object in __table_args__ for composite, partial, or expression indexes.
Code Example:
from sqlalchemy import Index
from sqlalchemy.orm import mapped_column, Mapped
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String(255), index=True) # simple index
name: Mapped[str] = mapped_column(String(100))
age: Mapped[int] = mapped_column()
deleted_at: Mapped[Optional[datetime]] = mapped_column(default=None)
__table_args__ = (
# Composite index
Index("idx_name_age", "name", "age"),
# Unique composite index
Index("idx_email_unique", "email", unique=True),
# Partial index (PostgreSQL — index only non-deleted rows)
Index(
"idx_active_users",
"email",
postgresql_where="deleted_at IS NULL",
),
# Expression index (PostgreSQL)
Index("idx_lower_email", text("lower(email)")),
)
🔍 Interviewer Notes: Tests production database knowledge. Partial indexes are powerful for soft-delete patterns.
❌ Common Mistakes:
- Over-indexing (write penalty) or under-indexing (slow reads)
- Not knowing that
unique=Trueon a column creates an index AND a constraint - Defining indexes in Python but forgetting to run migrations (Alembic doesn't auto-detect them)
Q25: What is sqlalchemy_utils and how do you install and import it?
Technical Answer:
sqlalchemy_utils is a third-party package extending SQLAlchemy with additional column types, utility functions, and helpers. It provides types like EmailType, URLType, UUIDType, PasswordType, TSVectorType, and functions like database_exists(), create_database(), drop_database().
Code Example:
# Install
# pip install sqlalchemy-utils
from sqlalchemy_utils import (
# Types
EmailType,
URLType,
UUIDType,
PasswordType,
ChoiceType,
ArrowType,
TSVectorType,
ColorType,
PhoneNumberType,
# Functions
database_exists,
create_database,
drop_database,
# Decorators
aggregated,
observes,
)
# Check if DB exists before creating
url = "postgresql://user:pass@localhost/mydb"
if not database_exists(url):
create_database(url)
# Drop database
drop_database(url)
🔍 Interviewer Notes: Tests awareness of the ecosystem. sqlalchemy_utils saves significant boilerplate for common patterns.
❌ Common Mistakes:
- Installing
sqlalchemy_utilsbut not knowing which version supports your SQLAlchemy version - Using
PasswordTypewithout configuring a hashing library (requirespasslib) - Applying
TSVectorTypeoutside PostgreSQL (it's PG-specific)
Q26: How do you use EmailType, URLType, and UUIDType from sqlalchemy_utils?
Technical Answer:
These types add validation and normalization. EmailType normalizes email to lowercase; URLType validates URL format; UUIDType stores UUIDs natively as UUID in PostgreSQL or CHAR(32) in other databases.
Code Example:
from sqlalchemy_utils import EmailType, URLType, UUIDType
import uuid
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(EmailType, nullable=False, unique=True)
website: Mapped[Optional[str]] = mapped_column(URLType)
public_id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), # stored as CHAR(32) — readable in DB
default=uuid.uuid4,
unique=True,
)
# Usage
with Session(engine) as session:
user = User(
email="ALICE@EXAMPLE.COM", # stored as "alice@example.com"
website="https://example.com",
public_id=uuid.uuid4(),
)
session.add(user)
session.commit()
🔍 Interviewer Notes: Shows awareness of data normalization at the ORM layer vs application layer.
❌ Common Mistakes:
- Not knowing
EmailTypenormalizes to lowercase (causes login bugs if validation is case-sensitive elsewhere) - Using
UUIDType(binary=True)(stores as BINARY(16)) vsbinary=False(CHAR(32)) without understanding the tradeoffs - Forgetting to add
nullable=Falsefor required fields
Q27: How do you use PasswordType from sqlalchemy_utils?
Technical Answer:
PasswordType automatically hashes passwords on assignment and verifies them on comparison. It requires passlib and a list of supported hashing schemes. Passwords are never stored in plain text.
Code Example:
# pip install passlib bcrypt
from sqlalchemy_utils import PasswordType
from passlib.context import CryptContext
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String(255), unique=True)
password: Mapped[str] = mapped_column(
PasswordType(schemes=["bcrypt"], deprecated="auto"),
nullable=False,
)
# Usage
with Session(engine) as session:
user = User(email="alice@example.com", password="mysecretpassword")
# password is automatically hashed on assignment
session.add(user)
session.commit()
# Verification — compare plain text to hash
fetched = session.scalar(select(User).where(User.email == "alice@example.com"))
print(fetched.password == "mysecretpassword") # True
print(fetched.password == "wrongpassword") # False
🔍 Interviewer Notes: Tests security awareness. The automatic hashing prevents the common bug of storing plain text by accident.
❌ Common Mistakes:
- Storing the raw hash string and doing comparison outside the ORM (bypasses
PasswordTypelogic) - Not configuring
deprecated="auto"for hash migration - Forgetting that
PasswordTypevalues are not JSON-serializable — need custom serializer
Q28: How do you use database_exists(), create_database(), and drop_database()?
Technical Answer:
These sqlalchemy_utils functions operate at the database level (not table level). They're useful in test setup/teardown, CI pipelines, and provisioning scripts where you need to create a fresh database programmatically.
Code Example:
from sqlalchemy_utils import database_exists, create_database, drop_database
from sqlalchemy import create_engine
DATABASE_URL = "postgresql://user:pass@localhost/testdb"
# Safe create
if not database_exists(DATABASE_URL):
create_database(DATABASE_URL)
print("Database created")
else:
print("Database already exists")
# Create all tables
engine = create_engine(DATABASE_URL)
Base.metadata.create_all(engine)
# Teardown
drop_database(DATABASE_URL)
# Pytest fixture example
import pytest
@pytest.fixture(scope="session")
def test_db():
if not database_exists(DATABASE_URL):
create_database(DATABASE_URL)
Base.metadata.create_all(create_engine(DATABASE_URL))
yield
drop_database(DATABASE_URL)
🔍 Interviewer Notes: Often used in integration test infrastructure. Shows ability to write proper test isolation.
❌ Common Mistakes:
- Calling
drop_database()without safety checks in production scripts - Not separating test DB URL from production URL (environment variable discipline)
- Using
create_all()for schema migrations instead of Alembic
Q29: What is sessionmaker and why should you use it?
Technical Answer:
sessionmaker is a factory that creates Session objects with a consistent configuration. Rather than passing the same arguments every time you create a Session, you configure once and reuse the factory. This is the standard pattern for applications.
Code Example:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
engine = create_engine("postgresql+psycopg2://user:pass@localhost/mydb")
# Configure factory once
SessionLocal = sessionmaker(
bind=engine,
autocommit=False,
autoflush=True,
expire_on_commit=True,
)
# FastAPI dependency pattern
def get_db():
db: Session = SessionLocal()
try:
yield db
finally:
db.close()
# Flask application factory pattern
def create_app():
app = Flask(__name__)
app.SessionLocal = sessionmaker(bind=engine)
return app
# Direct usage
with SessionLocal() as session:
users = session.scalars(select(User)).all()
🔍 Interviewer Notes: The FastAPI dependency pattern using sessionmaker is a standard interview talking point for Python web devs.
❌ Common Mistakes:
- Creating a new engine per request instead of reusing one engine
- Forgetting
db.close()in thefinallyblock (connection leak) - Setting
expire_on_commit=Falsewithout understanding the staleness implications
Q30: What is scoped_session and when should you use it?
Technical Answer:
scoped_session creates a thread-local (or context-local) session registry. All code in the same thread gets the same Session without explicitly passing it around. This is useful in Flask and similar synchronous frameworks but is NOT appropriate for async code.
Code Example:
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import create_engine
engine = create_engine("postgresql+psycopg2://user:pass@localhost/mydb")
session_factory = sessionmaker(bind=engine)
ScopedSession = scoped_session(session_factory)
# Thread-1 and Thread-2 each get their own Session
# within a single thread, all calls return the same session
# Flask example — all code in one request sees same session
@app.teardown_appcontext
def shutdown_session(exception=None):
ScopedSession.remove() # MUST call remove() at end of request
# Usage in model layer
def get_user(user_id: int):
return ScopedSession.get(User, user_id)
# WARNING: Never use scoped_session with asyncio
# Use AsyncSession + async_sessionmaker instead
🔍 Interviewer Notes: Misusing scoped_session in async code is a source of subtle bugs. Tests awareness of threading vs async context.
❌ Common Mistakes:
- Forgetting to call
.remove()at the end of each request (sessions accumulate) - Using
scoped_sessionin async FastAPI (thread-local doesn't work with coroutines) - Passing
scoped_sessioninstance directly asSessiontype (it proxies but isn't aSession)
Q31: How do you use hybrid_property in SQLAlchemy?
Technical Answer:
@hybrid_property defines a property that behaves differently at the Python instance level vs the SQL expression level. At the instance level it's a regular Python property; at the class level it returns a SQL expression usable in queries.
Code Example:
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy import func
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
first_name: Mapped[str] = mapped_column(String(100))
last_name: Mapped[str] = mapped_column(String(100))
_password: Mapped[str] = mapped_column("password", String(255))
@hybrid_property
def full_name(self) -> str:
return f"{self.first_name} {self.last_name}"
@full_name.expression
@classmethod
def full_name(cls):
return func.concat(cls.first_name, " ", cls.last_name)
@hybrid_property
def password(self):
return self._password
@password.setter
def password(self, value: str):
self._password = hash_password(value)
# Usage
user = User(first_name="Alice", last_name="Smith")
print(user.full_name) # "Alice Smith" — Python property
# SQL-level filtering
stmt = select(User).where(User.full_name == "Alice Smith")
# generates: WHERE concat(first_name, ' ', last_name) = 'Alice Smith'
🔍 Interviewer Notes: Tests ORM extension knowledge. hybrid_property is the right solution for computed columns that need to be filterable.
❌ Common Mistakes:
- Defining only the Python-level property without the
.expression(can't use in queries) - Using
@hybrid_propertyfor expensive computations (no caching — called every access) - Forgetting the
@classmethoddecorator on the expression method (required in 2.0)
Q32: How do you use column_property() for computed columns?
Technical Answer:
column_property() maps a SQL expression to a mapped attribute. Unlike hybrid_property, it's always evaluated as SQL (loaded with the row). It's best for simple expressions you always want to load.
Code Example:
from sqlalchemy.orm import column_property
from sqlalchemy import select, func, case
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
subtotal: Mapped[float] = mapped_column(Numeric(10, 2))
tax_rate: Mapped[float] = mapped_column(Numeric(5, 4), default=0.1)
# Computed: subtotal * (1 + tax_rate)
total = column_property(
subtotal * (1 + tax_rate)
)
# Correlated subquery as column_property
# (loaded as a subquery in the SELECT)
# Access like a normal attribute
with Session(engine) as session:
order = session.get(Order, 1)
print(order.total) # computed by DB, not Python
🔍 Interviewer Notes: Distinguishes column_property (always SQL, always loaded) from hybrid_property (Python OR SQL depending on context).
❌ Common Mistakes:
- Using
column_propertyfor expressions involving other tables (usehybrid_propertywith a subquery instead) - Not knowing that
column_propertyexpressions are included in every SELECT (adds overhead) - Confusing with
@property(pure Python, not usable in SQL queries)
Q33: How do you validate model data using SQLAlchemy @validates?
Technical Answer:
The @validates decorator intercepts attribute assignments and allows validation or transformation before the value is stored. It runs on the Python object before any DB interaction.
Code Example:
from sqlalchemy.orm import validates
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String(255), unique=True)
age: Mapped[int] = mapped_column()
role: Mapped[str] = mapped_column(String(50))
@validates("email")
def validate_email(self, key, value):
assert "@" in value, "Invalid email address"
return value.lower().strip()
@validates("age")
def validate_age(self, key, value):
if value < 0 or value > 150:
raise ValueError(f"Age {value} is not realistic")
return value
@validates("role")
def validate_role(self, key, value):
allowed = {"admin", "user", "moderator"}
if value not in allowed:
raise ValueError(f"Role must be one of {allowed}")
return value
# Usage
user = User(email="ALICE@EXAMPLE.COM", age=25, role="admin")
print(user.email) # "alice@example.com" — normalized
🔍 Interviewer Notes: @validates runs before flush, which is different from DB constraints that run at commit. Good for catching errors early with readable Python exceptions.
❌ Common Mistakes:
- Using
@validatesas a replacement for DB constraints (not enforced if bypassing ORM) - Forgetting to
return value(attribute becomesNone) - Not handling the case where the validator is called with
Nonefor optional fields
Q34: How do you implement soft delete in SQLAlchemy?
Technical Answer:
Soft delete marks rows as deleted without removing them. Common patterns include a deleted_at timestamp column, a is_deleted boolean, or overriding the Query class to filter by default.
Code Example:
from datetime import datetime
from sqlalchemy import event
from sqlalchemy.orm import Query
class SoftDeleteMixin:
deleted_at: Mapped[Optional[datetime]] = mapped_column(default=None, index=True)
def soft_delete(self):
self.deleted_at = datetime.utcnow()
@property
def is_deleted(self) -> bool:
return self.deleted_at is not None
class Post(Base, SoftDeleteMixin):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
# Filter out soft-deleted in queries
with Session(engine) as session:
active_posts = session.scalars(
select(Post).where(Post.deleted_at.is_(None))
).all()
post = session.get(Post, 1)
post.soft_delete()
session.commit()
# Hard delete (when needed)
with Session(engine) as session:
session.execute(
delete(Post).where(Post.deleted_at < datetime(2020, 1, 1))
)
session.commit()
🔍 Interviewer Notes: Soft delete is a common pattern. Tests schema design thinking and the tradeoff (storage vs auditability).
❌ Common Mistakes:
- Not indexing
deleted_at(full table scans on every query) - Forgetting to filter in all queries (accidentally showing deleted records)
- Not having a hard-delete cleanup job (unbounded table growth)
Q35: How do you use SQLAlchemy events?
Technical Answer:
SQLAlchemy's event system lets you hook into mapper, session, and connection lifecycle events. Use @event.listens_for(target, "event_name") or event.listen(). Common events: before_insert, after_insert, before_commit, after_commit, init.
Code Example:
from sqlalchemy import event
from sqlalchemy.orm import Session
# Mapper event — fires before every INSERT for User
@event.listens_for(User, "before_insert")
def set_defaults(mapper, connection, target):
if not target.public_id:
import uuid
target.public_id = str(uuid.uuid4())
# Session event — audit log on commit
@event.listens_for(Session, "before_commit")
def before_commit(session):
for obj in session.new:
print(f"Inserting: {obj}")
for obj in session.dirty:
print(f"Updating: {obj}")
for obj in session.deleted:
print(f"Deleting: {obj}")
# Engine event — log slow queries
from sqlalchemy import event as sa_event
@sa_event.listens_for(engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, statement, params, context, executemany):
import time
context._query_start_time = time.time()
@sa_event.listens_for(engine, "after_cursor_execute")
def after_cursor_execute(conn, cursor, statement, params, context, executemany):
total = time.time() - context._query_start_time
if total > 0.5:
print(f"SLOW QUERY ({total:.2f}s): {statement}")
🔍 Interviewer Notes: Events are used for audit logging, automatic field population, and monitoring. Tests architectural knowledge beyond basic CRUD.
❌ Common Mistakes:
- Doing heavy work in
before_commitevents (blocks transaction) - Not removing event listeners when they're no longer needed (memory leak)
- Modifying session state inside session events (can cause infinite recursion)
Q36: How do you use relationship with cascade options?
Technical Answer:
cascade controls what operations propagate from parent to child objects. Common values: "all", "all, delete-orphan", "save-update, merge", "delete". "delete-orphan" removes children when they're no longer associated with a parent.
Code Example:
from sqlalchemy.orm import relationship
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# Cascade delete — deleting User deletes all posts
posts: Mapped[List["Post"]] = relationship(
back_populates="author",
cascade="all, delete-orphan",
passive_deletes=True, # let DB handle cascades via FK
)
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
author_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
author: Mapped["User"] = relationship(back_populates="posts")
# Usage
with Session(engine) as session:
user = session.get(User, 1)
session.delete(user)
session.commit() # also deletes all user.posts
🔍 Interviewer Notes: passive_deletes=True + DB-level ON DELETE CASCADE is more efficient than SQLAlchemy-managed cascades for large related sets.
❌ Common Mistakes:
- Using
cascade="all"withoutdelete-orphan(orphaned children remain after disassociation) - Not setting
ondelete="CASCADE"on the FK when usingpassive_deletes=True(DB doesn't cascade, rows remain) - Cascade deleting across large relationships (SELECT then DELETE per row — use bulk DELETE instead)
Q37: How do you use Session.merge() and when is it needed?
Technical Answer:
session.merge(instance) reconciles a detached or transient object with the session. It looks up the object by primary key: if found in session, updates it; if not, loads from DB; if not in DB, prepares a new insert. Useful for importing data or reattaching objects across sessions.
Code Example:
# Pattern: object from one session used in another
with Session(engine) as session1:
user = session1.get(User, 1)
# user is now detached
with Session(engine) as session2:
# DON'T: session2.add(user) — raises error if already in DB
# DO:
user.name = "Updated Name"
merged_user = session2.merge(user) # SQLAlchemy handles the state
session2.commit()
# Also useful for upsert-like behavior from external data
external_data = {"id": 42, "name": "External User", "email": "ext@example.com"}
with Session(engine) as session:
user = User(**external_data)
merged = session.merge(user) # insert if new, update if exists
session.commit()
🔍 Interviewer Notes: merge() vs add() vs direct attribute update — knowing which to use when prevents InvalidRequestError.
❌ Common Mistakes:
- Using
session.add()on a detached persistent object (raisesInvalidRequestError) - Assuming
merge()always hits the database (it may use the identity map) - Using
merge()for true upsert semantics in concurrent code (race conditions — useINSERT ... ON CONFLICTinstead)
Q38: How do you query with subqueries in SQLAlchemy?
Technical Answer:
Use .subquery() to create a named subquery, or .scalar_subquery() for a scalar correlated subquery. In SQLAlchemy 2.0, these replace the legacy .as_scalar() method.
Code Example:
from sqlalchemy import select, func
# Subquery in FROM
avg_salary_sq = (
select(func.avg(Employee.salary).label("avg"))
.subquery()
)
stmt = select(Employee).where(Employee.salary > avg_salary_sq.c.avg)
# Correlated scalar subquery
post_count_sq = (
select(func.count(Post.id))
.where(Post.author_id == User.id)
.correlate(User)
.scalar_subquery()
)
stmt = select(User, post_count_sq.label("post_count"))
# EXISTS subquery
from sqlalchemy import exists
has_posts = exists().where(Post.author_id == User.id)
stmt = select(User).where(has_posts)
with Session(engine) as session:
users_with_posts = session.scalars(stmt).all()
🔍 Interviewer Notes: Subquery vs JOIN is a common optimization discussion. Correlated subqueries in SELECT are often replaceable with LEFT JOIN + GROUP BY for better performance.
❌ Common Mistakes:
- Using
.as_scalar()(removed in 2.0) instead of.scalar_subquery() - Not using
.correlate()when needed (generates wrong SQL) - Using correlated subqueries in SELECT for large datasets (N queries in disguise)
Q39: How do you use Common Table Expressions (CTEs) in SQLAlchemy?
Technical Answer:
.cte() creates a named CTE from any select() statement. CTEs improve readability and allow recursive queries. Recursive CTEs use union_all() to build the recursive part.
Code Example:
from sqlalchemy import select, literal
# Simple CTE
recent_users_cte = (
select(User)
.where(User.created_at > datetime(2024, 1, 1))
.cte("recent_users")
)
stmt = select(recent_users_cte).where(recent_users_cte.c.age > 18)
# Recursive CTE (org chart traversal)
org_cte = (
select(Employee.id, Employee.name, Employee.manager_id)
.where(Employee.manager_id.is_(None)) # base case: top-level
.cte("org_tree", recursive=True)
)
recursive_part = (
select(Employee.id, Employee.name, Employee.manager_id)
.join(org_cte, Employee.manager_id == org_cte.c.id)
)
org_cte = org_cte.union_all(recursive_part)
with Session(engine) as session:
tree = session.execute(select(org_cte)).all()
🔍 Interviewer Notes: CTEs and recursive queries are senior-level SQL knowledge. Tests ability to translate complex SQL patterns to SQLAlchemy.
❌ Common Mistakes:
- Not knowing
.cte()exists and writing raw SQL for these patterns - Forgetting
recursive=Truefor recursive CTEs - Using CTEs where a simple JOIN would be more efficient
Q40: How do you handle database schema changes across multiple environments?
Technical Answer:
Use Alembic for schema migrations. Alembic integrates with SQLAlchemy models and generates versioned migration scripts. Key commands: alembic revision --autogenerate, alembic upgrade head, alembic downgrade -1.
Code Example:
# alembic.ini connection string
# sqlalchemy.url = postgresql://user:pass@localhost/mydb
# env.py — point to your models
from myapp.models import Base
target_metadata = Base.metadata
# Generate migration
# alembic revision --autogenerate -m "add users table"
# Generated migration (alembic/versions/xxxx_add_users_table.py)
def upgrade() -> None:
op.create_table(
"users",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(100), nullable=False),
sa.Column("email", sa.String(255), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("email"),
)
def downgrade() -> None:
op.drop_table("users")
# Apply
# alembic upgrade head
# Rollback one step
# alembic downgrade -1
🔍 Interviewer Notes: No production SQLAlchemy app should use create_all() for schema management. Alembic knowledge is expected for any backend role.
❌ Common Mistakes:
- Running
Base.metadata.create_all()in production instead of Alembic migrations - Not checking auto-generated migrations before running (autogenerate misses some changes)
- Not keeping migration scripts in version control
Q41: How do you perform bulk inserts efficiently?
Technical Answer:
For large inserts, use session.execute(insert(Model), list_of_dicts) or Core's insert().values(). Avoid session.add_all() for thousands of rows — it loads each object into the identity map.
Code Example:
from sqlalchemy import insert
import time
# SLOW: session.add_all() — creates Python objects + identity map entries
with Session(engine) as session:
users = [User(name=f"User {i}", email=f"user{i}@example.com") for i in range(10000)]
session.add_all(users)
session.commit() # slow for large volumes
# FAST: bulk insert with list of dicts (no ORM overhead)
with Session(engine) as session:
session.execute(
insert(User),
[{"name": f"User {i}", "email": f"user{i}@example.com"} for i in range(10000)]
)
session.commit()
# FASTEST: Core insert with executemany
with engine.begin() as conn:
conn.execute(
insert(User.__table__),
[{"name": f"User {i}", "email": f"user{i}@example.com"} for i in range(10000)]
)
🔍 Interviewer Notes: 10x–100x performance difference between ORM add_all and bulk insert for large datasets. This is a critical production knowledge point.
❌ Common Mistakes:
- Using
session.add_all()for thousands of rows (memory + speed issue) - Not batching large inserts (memory pressure — batch in chunks of 1000–5000)
- Not using
COPY(psycopg2) orinsert().on_conflict_do_nothing()for even faster loads
Q42: How do you perform bulk updates efficiently?
Technical Answer:
Use session.execute(update(Model).where(...).values(...)) for set-based updates. This generates a single UPDATE SQL statement instead of loading objects and updating them one by one.
Code Example:
from sqlalchemy import update
with Session(engine) as session:
# SLOW: ORM object-by-object update
users = session.scalars(select(User).where(User.age < 18)).all()
for user in users:
user.status = "minor"
session.commit()
# FAST: Single UPDATE statement
session.execute(
update(User)
.where(User.age < 18)
.values(status="minor")
)
session.commit()
# Update with expression
from sqlalchemy import func
session.execute(
update(User)
.values(login_count=User.login_count + 1)
.where(User.id.in_([1, 2, 3, 4, 5]))
)
session.commit()
# WARNING: objects already in session are NOT auto-refreshed
# Call session.expire_all() or re-query after bulk update
session.expire_all()
🔍 Interviewer Notes: The stale cache issue after bulk updates is a production gotcha. Tests understanding of SQLAlchemy's identity map.
❌ Common Mistakes:
- Not calling
session.expire_all()after bulk update (in-session objects show stale data) - Using
synchronize_session="fetch"(loads all matching rows into Python — defeats the purpose) - Forgetting
WHEREclause (UPDATE without WHERE updates all rows)
Q43: How do you use delete() for bulk deletion?
Technical Answer:
session.execute(delete(Model).where(...)) generates a single DELETE SQL statement. Like bulk update, use session.expire_all() after if any of the deleted objects are loaded in the session.
Code Example:
from sqlalchemy import delete
with Session(engine) as session:
# Bulk delete with condition
session.execute(
delete(Post).where(Post.created_at < datetime(2020, 1, 1))
)
session.commit()
# Delete with subquery (delete users with no posts)
session.execute(
delete(User).where(
~User.id.in_(select(Post.author_id).where(Post.author_id.is_not(None)))
)
)
session.commit()
# Returning deleted rows (PostgreSQL)
deleted = session.execute(
delete(Post)
.where(Post.status == "draft")
.returning(Post.id, Post.title)
).fetchall()
session.commit()
print(f"Deleted {len(deleted)} draft posts")
🔍 Interviewer Notes: .returning() is PostgreSQL-specific but extremely useful for audit trails without a second query.
❌ Common Mistakes:
- Using
session.delete(obj)for bulk deletion (loads each object first) - Not considering FK constraints (delete parent without deleting children causes FK violation)
- Forgetting
synchronize_session="evaluate"behavior (default — SQLAlchemy evaluates WHERE in Python too)
Q44: How do you implement pagination with total count efficiently?
Technical Answer:
Efficient pagination returns both the page of results and the total count. Use a COUNT(*) query with the same filters (minus LIMIT/OFFSET) and run both queries together. Avoid len(all_results).
Code Example:
from sqlalchemy import select, func
def paginate(session: Session, stmt, page: int, per_page: int):
# Count query — same filters, no LIMIT/OFFSET
count_stmt = select(func.count()).select_from(stmt.subquery())
total = session.scalar(count_stmt)
# Data query
results = session.scalars(
stmt.limit(per_page).offset((page - 1) * per_page)
).all()
return {
"items": results,
"total": total,
"page": page,
"per_page": per_page,
"pages": (total + per_page - 1) // per_page,
}
# Usage
with Session(engine) as session:
base_stmt = select(User).where(User.is_active == True).order_by(User.name)
result = paginate(session, base_stmt, page=2, per_page=20)
🔍 Interviewer Notes: The count_stmt from a subquery pattern is a clean approach. Tests SQL optimization awareness.
❌ Common Mistakes:
- Fetching all results and slicing in Python
- Running
COUNT(*)without the same WHERE filters (wrong totals) - Not including
ORDER BYin paginated queries (non-deterministic page content)
Q45: How do you use exists() in SQLAlchemy?
Technical Answer:
exists() generates a SQL EXISTS subquery, which is often faster than COUNT > 0 or fetching a full row just to check presence. Use it in .where() conditions.
Code Example:
from sqlalchemy import exists, select
with Session(engine) as session:
# Check if a user has any posts
has_posts = exists().where(Post.author_id == User.id)
stmt = select(User).where(has_posts)
users_with_posts = session.scalars(stmt).all()
# NOT EXISTS
stmt = select(User).where(~exists().where(Post.author_id == User.id))
users_without_posts = session.scalars(stmt).all()
# Scalar exists check
user_exists = session.scalar(
select(exists().where(User.email == "alice@example.com"))
)
print(user_exists) # True or False
# exists() vs count — exists stops at first row (faster)
# count(*) scans all matching rows
🔍 Interviewer Notes: EXISTS vs COUNT > 0 is a classic SQL optimization interview topic.
❌ Common Mistakes:
- Using
session.scalar(select(User).where(...))and checking forNoneinstead ofexists() - Wrapping
exists()inselect(func.count(...))unnecessarily - Not using
~exists()forNOT EXISTS(using!= Trueon the result instead)
Q46: How do you handle database transactions with savepoints?
Technical Answer:
Savepoints allow partial rollback within a transaction. In SQLAlchemy, use session.begin_nested() to create a nested transaction (savepoint). Rolling back the nested transaction only undoes changes since the savepoint.
Code Example:
with Session(engine) as session:
with session.begin():
user = User(name="Alice")
session.add(user)
try:
with session.begin_nested(): # creates SAVEPOINT
bad_post = Post(title="Bad", author_id=None) # will violate FK
session.add(bad_post)
session.flush() # raises IntegrityError
except Exception:
# Only the nested transaction is rolled back (to savepoint)
# user is still pending
pass
# User is still in the transaction
session.add(Post(title="Good Post", author_id=user.id))
# Commits both user and good post
🔍 Interviewer Notes: Savepoints are important for retry logic and partial failure handling in complex workflows.
❌ Common Mistakes:
- Not knowing
begin_nested()exists and rolling back the entire transaction for partial failures - Using savepoints in SQLite (limited support — only with WAL mode)
- Forgetting that
flush()is needed to trigger the IntegrityError within the nested context
Q47: How do you handle NULL values and default values in SQLAlchemy?
Technical Answer:
default sets a Python-side default (evaluated when the row is created). server_default sets a database-side default (in the DDL). onupdate provides a Python-side default for UPDATE operations. nullable=False enforces NOT NULL.
Code Example:
from sqlalchemy import String, DateTime, Boolean, func
from datetime import datetime
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200), nullable=False)
slug: Mapped[str] = mapped_column(String(200), unique=True)
is_published: Mapped[bool] = mapped_column(Boolean, default=False)
view_count: Mapped[int] = mapped_column(default=0)
created_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow, # Python default (evaluated at insert time)
# OR: server_default=func.now() # DB default (in DDL — more reliable)
)
updated_at: Mapped[Optional[datetime]] = mapped_column(
DateTime,
onupdate=datetime.utcnow, # auto-set on UPDATE
)
🔍 Interviewer Notes: default=datetime.utcnow (function ref) vs default=datetime.utcnow() (called once at import) is a classic Python gotcha.
❌ Common Mistakes:
default=datetime.utcnow()— calls it once at class definition time (all rows get same timestamp)- Not using
server_defaultfor DB-generated values (Python default won't be set on rows inserted outside the ORM) - Forgetting
onupdateforupdated_at(must be set explicitly on every update)
Q48: How do you use __table_args__ correctly?
Technical Answer:
__table_args__ can be a tuple of constraint/index objects, or a dict of table-level keyword arguments, or a tuple ending with a dict that combines both. It must be a tuple (not a list) when mixing constraints.
Code Example:
from sqlalchemy import UniqueConstraint, Index, CheckConstraint
class Payment(Base):
__tablename__ = "payments"
__table_args__ = (
# Constraints
UniqueConstraint("order_id", "attempt_number", name="uq_payment_attempt"),
CheckConstraint("amount > 0", name="chk_positive_amount"),
Index("idx_payments_status_created", "status", "created_at"),
# Table kwargs as the LAST element (dict)
{"schema": "billing", "comment": "Payment records"},
)
id: Mapped[int] = mapped_column(primary_key=True)
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
attempt_number: Mapped[int] = mapped_column(default=1)
amount: Mapped[float] = mapped_column(Numeric(10, 2))
status: Mapped[str] = mapped_column(String(50))
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
🔍 Interviewer Notes: The tuple-ending-with-dict pattern is a frequent source of bugs. Tests attention to SQLAlchemy API details.
❌ Common Mistakes:
- Using a list instead of tuple (raises
ArgumentError) - Putting the dict in the middle instead of last
- Defining only a single constraint directly as a dict (works but prevents adding more)
Q49: How do you handle composite primary keys?
Technical Answer:
Composite PKs are defined by setting primary_key=True on multiple columns. In Mapped annotations, both columns need mapped_column(primary_key=True). ORM operations use all PK columns for identity.
Code Example:
class OrderItem(Base):
__tablename__ = "order_items"
order_id: Mapped[int] = mapped_column(
ForeignKey("orders.id"), primary_key=True
)
product_id: Mapped[int] = mapped_column(
ForeignKey("products.id"), primary_key=True
)
quantity: Mapped[int] = mapped_column(default=1)
unit_price: Mapped[float] = mapped_column(Numeric(10, 2))
order: Mapped["Order"] = relationship()
product: Mapped["Product"] = relationship()
# Access with composite PK
with Session(engine) as session:
item = session.get(OrderItem, {"order_id": 1, "product_id": 42})
# Or using tuple (positional, in column definition order)
item = session.get(OrderItem, (1, 42))
🔍 Interviewer Notes: Composite PKs are used in association tables. Accessing by tuple vs dict is a common gotcha.
❌ Common Mistakes:
- Trying to use
session.get(OrderItem, 1)with a single value (raises error) - Forgetting to include all PK columns in
session.get()identity arguments - Using a surrogate auto-increment PK on all tables when composite PK is more natural (e.g., junction tables)
Q50: How do you use select() with multiple tables and column selection?
Technical Answer:
Pass multiple models or columns to select(). Use .label() to rename columns. Access results by position, by attribute, or as a Row named tuple.
Code Example:
from sqlalchemy import select
with Session(engine) as session:
# Select specific columns
stmt = select(User.id, User.name, User.email).where(User.is_active == True)
rows = session.execute(stmt).all()
for row in rows:
print(row.id, row.name, row.email) # Row namedtuple access
# Select from multiple tables
stmt = (
select(
User.name.label("author"),
Post.title,
func.count(Comment.id).label("comment_count"),
)
.join(Post, Post.author_id == User.id)
.outerjoin(Comment, Comment.post_id == Post.id)
.group_by(User.name, Post.title)
.order_by(func.count(Comment.id).desc())
)
rows = session.execute(stmt).all()
for row in rows:
print(f"{row.author}: '{row.title}' ({row.comment_count} comments)")
🔍 Interviewer Notes: Understanding session.scalars() (returns first column) vs session.execute() (returns full rows) is essential.
❌ Common Mistakes:
- Using
session.scalars()when selecting multiple columns (returns only first column) - Not using
.label()when two columns from different tables share the same name - Accessing
row[0]by index instead of by attribute name (fragile code)
Q51: How do you use aliased() for self-joins and query disambiguation?
Technical Answer:
aliased() creates an alias of a mapped class, allowing the same table to appear multiple times in a query (self-joins) or disambiguating duplicate table references in complex queries.
Code Example:
from sqlalchemy.orm import aliased
Manager = aliased(Employee, name="manager")
stmt = (
select(Employee, Manager)
.join(Manager, Employee.manager_id == Manager.id)
.where(Manager.department == "Engineering")
)
with Session(engine) as session:
rows = session.execute(stmt).all()
for emp, mgr in rows:
print(f"{emp.name} reports to {mgr.name}")
🔍 Interviewer Notes: Self-joins without aliased() produce ambiguous SQL. Tests understanding of table aliasing at the ORM level.
❌ Common Mistakes:
- Joining
EmployeetoEmployeedirectly without aliasing (SQLAlchemy uses the same table twice — ambiguous) - Forgetting the
name=parameter (makes SQLEXPLAINoutput readable) - Using
aliased()when a simplerelationship()withremote_sidewould be cleaner
Q52: How do you use union() and union_all() in SQLAlchemy?
Technical Answer:
union() combines results from two select() statements and removes duplicates. union_all() keeps duplicates and is faster. Both require matching column sets.
Code Example:
from sqlalchemy import union, union_all, select
admins_stmt = select(User.id, User.name).where(User.role == "admin")
mods_stmt = select(User.id, User.name).where(User.role == "moderator")
# UNION (distinct)
combined = union(admins_stmt, mods_stmt)
# UNION ALL (keep duplicates, faster)
combined_all = union_all(admins_stmt, mods_stmt)
with Session(engine) as session:
rows = session.execute(combined).all()
for row in rows:
print(row.id, row.name)
# As a subquery
sq = combined.subquery()
stmt = select(sq).order_by(sq.c.name)
🔍 Interviewer Notes: union_all is preferred when duplicates are impossible or acceptable — avoids the DISTINCT sort cost.
❌ Common Mistakes:
- Mismatched column counts or types between the two selects
- Using
union()on large datasets when duplicates aren't possible (unnecessary dedup cost) - Forgetting to
.subquery()before ordering aUNIONresult
Q53: How do you implement window functions in SQLAlchemy?
Technical Answer:
Use func.<function>().over(partition_by=..., order_by=...) to create window function expressions. Common uses: row_number(), rank(), dense_rank(), lag(), lead(), sum() over a partition.
Code Example:
from sqlalchemy import func, select
stmt = select(
Employee.id,
Employee.name,
Employee.department,
Employee.salary,
func.row_number().over(
partition_by=Employee.department,
order_by=Employee.salary.desc()
).label("rank_in_dept"),
func.sum(Employee.salary).over(
partition_by=Employee.department
).label("dept_total"),
func.lag(Employee.salary).over(
partition_by=Employee.department,
order_by=Employee.salary
).label("prev_salary"),
)
with Session(engine) as session:
rows = session.execute(stmt).all()
for row in rows:
print(f"{row.name}: rank {row.rank_in_dept} in {row.department}")
🔍 Interviewer Notes: Window functions are a seniority differentiator. Tests ability to express complex analytics in SQLAlchemy without dropping to raw SQL.
❌ Common Mistakes:
- Trying to filter on window function results in
WHERE(use a subquery or CTE — window functions are post-aggregation) - Confusing
rank()(gaps after ties) withdense_rank()(no gaps) - Not knowing
func.row_number().over()syntax and writing rawtext()instead
Q54: How do you use cast() and type_coerce() for type conversions?
Technical Answer:
cast() generates a SQL CAST expression. type_coerce() tells SQLAlchemy how to treat a Python value without emitting a SQL cast — useful when the DB handles conversion implicitly.
Code Example:
from sqlalchemy import cast, type_coerce, Integer, String, Numeric
from sqlalchemy.dialects.postgresql import JSONB
# cast() — emits SQL CAST
stmt = select(cast(User.age, String)).where(cast(User.id, String).like("1%"))
# Numeric precision
stmt = select(cast(Product.price, Numeric(10, 2)))
# type_coerce() — no SQL cast, just Python-side type hint
# Useful for JSON columns
raw_json = '{"key": "value"}'
stmt = select(User).where(
User.meta["key"].as_string() == type_coerce("value", String)
)
with Session(engine) as session:
results = session.execute(stmt).all()
🔍 Interviewer Notes: cast() vs type_coerce() is a subtle but important distinction for PostgreSQL JSON/JSONB query patterns.
❌ Common Mistakes:
- Using
cast()for JSON key access (use.as_string(),.as_integer()instead) - Not knowing
type_coerce()exists and adding unnecessary SQL casts - Casting unnecessarily on indexed columns (defeats index use)
Q55: How do you work with JSON and JSONB columns?
Technical Answer:
SQLAlchemy's JSON type works across databases. PostgreSQL's JSONB type additionally supports indexing. Use subscript notation or .op() for key access and filtering.
Code Example:
from sqlalchemy import JSON
from sqlalchemy.dialects.postgresql import JSONB
class Config(Base):
__tablename__ = "configs"
id: Mapped[int] = mapped_column(primary_key=True)
settings: Mapped[dict] = mapped_column(JSONB, default=dict)
tags: Mapped[list] = mapped_column(JSONB, default=list)
with Session(engine) as session:
# Insert JSON data
cfg = Config(settings={"theme": "dark", "lang": "en"}, tags=["beta", "internal"])
session.add(cfg)
session.commit()
# Query JSON key (PostgreSQL)
stmt = select(Config).where(Config.settings["theme"].as_string() == "dark")
# Query nested key
stmt = select(Config).where(
Config.settings["notifications"]["email"].as_boolean() == True
)
# JSONB contains operator (@>)
stmt = select(Config).where(
Config.settings.op("@>")('{"lang": "en"}')
)
# GIN index for JSONB
# Index("idx_config_settings_gin", Config.settings, postgresql_using="gin")
🔍 Interviewer Notes: JSONB with GIN index is a powerful pattern for semi-structured data. Tests PostgreSQL-specific SQLAlchemy knowledge.
❌ Common Mistakes:
- Using
JSONinstead ofJSONBin PostgreSQL (JSON is not indexable) - Forgetting
.as_string()/.as_integer()when comparing JSON values (type mismatch) - Not adding a GIN index on frequently queried JSONB columns
Q56: How do you use the Enum type in SQLAlchemy?
Technical Answer:
SQLAlchemy's Enum type maps to a database ENUM type (PostgreSQL) or VARCHAR with a CHECK constraint. Use Python's enum.Enum for type safety, or pass string values directly.
Code Example:
import enum
from sqlalchemy import Enum as SAEnum
class OrderStatus(enum.Enum):
PENDING = "pending"
CONFIRMED = "confirmed"
SHIPPED = "shipped"
DELIVERED = "delivered"
CANCELLED = "cancelled"
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
status: Mapped[OrderStatus] = mapped_column(
SAEnum(OrderStatus, name="order_status"), # named type in PostgreSQL
default=OrderStatus.PENDING,
)
with Session(engine) as session:
order = Order(status=OrderStatus.CONFIRMED)
session.add(order)
session.commit()
# Query by enum value
pending = session.scalars(
select(Order).where(Order.status == OrderStatus.PENDING)
).all()
🔍 Interviewer Notes: Named PostgreSQL ENUM types require migration care — adding/removing values needs ALTER TYPE. Tests schema design awareness.
❌ Common Mistakes:
- Storing enum as raw
StringwithoutSAEnum(loses DB-level constraint) - Not naming the PostgreSQL type (
name=) — causes anonymous type issues in Alembic - Forgetting that adding a new enum value in PostgreSQL requires
ALTER TYPE ... ADD VALUE
Q57: How do you work with Array columns (PostgreSQL)?
Technical Answer:
PostgreSQL ARRAY columns store ordered lists of a single type. Use sqlalchemy.dialects.postgresql.ARRAY with an element type. Supports indexing, containment operators, and any()/all() expressions.
Code Example:
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy import String, Integer
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
tags: Mapped[list] = mapped_column(ARRAY(String), default=list)
scores: Mapped[list] = mapped_column(ARRAY(Integer), default=list)
with Session(engine) as session:
post = Post(title="SQLAlchemy Tips", tags=["python", "orm", "sql"])
session.add(post)
session.commit()
# Filter: array contains element
stmt = select(Post).where(Post.tags.any("python"))
# Filter: array contains ALL elements (@>)
stmt = select(Post).where(Post.tags.contains(["python", "orm"]))
# Filter: overlap (&&)
stmt = select(Post).where(Post.tags.overlap(["python", "django"]))
# Array index access (1-based in PostgreSQL)
stmt = select(Post.tags[1]) # first element
🔍 Interviewer Notes: Tests PostgreSQL-specific knowledge. ARRAY vs JSONB for lists is a design trade-off question.
❌ Common Mistakes:
- Using Python 0-based indexing for PostgreSQL arrays (PostgreSQL arrays are 1-based)
- Not adding a GIN index for
&&and@>operators on large arrays - Using
ARRAYwhen a normalized child table would be more appropriate
Q58: How do you use association_proxy in SQLAlchemy?
Technical Answer:
association_proxy creates a shortcut attribute that traverses a relationship to access an attribute on the related object. Simplifies access to many-to-many or one-to-many data without loading intermediate objects manually.
Code Example:
from sqlalchemy.ext.associationproxy import association_proxy
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
memberships: Mapped[List["GroupMembership"]] = relationship()
# Proxy: user.groups -> [group.name for membership in memberships]
group_names = association_proxy("memberships", "group_name")
class GroupMembership(Base):
__tablename__ = "group_memberships"
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True)
group_id: Mapped[int] = mapped_column(ForeignKey("groups.id"), primary_key=True)
group: Mapped["Group"] = relationship()
group_name = association_proxy("group", "name")
with Session(engine) as session:
user = session.get(User, 1)
print(user.group_names) # ["Admins", "Beta Testers"] — no intermediate object
🔍 Interviewer Notes: association_proxy reduces boilerplate for traversing relationships. Tests ORM extension knowledge.
❌ Common Mistakes:
- Using
association_proxyon lazy-loaded relationships without eager loading (N+1) - Trying to filter on an
association_proxyvalue in a query (not directly usable inWHERE) - Forgetting that writes through
association_proxyrequire the creator pattern
Q59: How do you use deferred() loading for large columns?
Technical Answer:
deferred() marks a column to not be loaded in the initial SELECT. The value is fetched lazily on first access. Useful for large Text or LargeBinary columns you don't always need.
Code Example:
from sqlalchemy.orm import deferred, load_only, defer, undefer
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
# body is NOT loaded in default SELECT
body: Mapped[str] = mapped_column(Text, deferred=True)
thumbnail: Mapped[bytes] = mapped_column(LargeBinary, deferred=True)
with Session(engine) as session:
# Default query — body and thumbnail NOT loaded
articles = session.scalars(select(Article)).all()
for a in articles:
print(a.title) # no extra query
# print(a.body) # triggers extra SELECT per article — N+1!
# Explicitly load deferred columns when needed
stmt = select(Article).options(undefer(Article.body))
full_articles = session.scalars(stmt).all()
# Load ONLY specific columns (exclude others)
stmt = select(Article).options(load_only(Article.id, Article.title))
🔍 Interviewer Notes: Critical for API endpoints that list articles/products — avoid loading large blobs in list views.
❌ Common Mistakes:
- Accessing deferred columns in a loop (N+1 — use
undefer()in the query instead) - Not knowing
load_only()exists (loading all columns including large ones) - Deferring columns that are frequently needed (false optimization)
Q60: How do you test SQLAlchemy code with pytest?
Technical Answer: Best practice: use a real test database (not mocks), create all tables in a session-scoped fixture, and wrap each test in a transaction that rolls back. This gives fast isolation without dropping/recreating tables.
Code Example:
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
TEST_DB_URL = "postgresql://user:pass@localhost/test_myapp"
@pytest.fixture(scope="session")
def engine():
eng = create_engine(TEST_DB_URL)
Base.metadata.create_all(eng)
yield eng
Base.metadata.drop_all(eng)
@pytest.fixture
def session(engine):
connection = engine.connect()
transaction = connection.begin()
session = Session(bind=connection)
yield session
session.close()
transaction.rollback() # undo all changes after each test
connection.close()
# Test
def test_create_user(session):
user = User(name="Test", email="test@example.com")
session.add(user)
session.flush()
assert user.id is not None
fetched = session.get(User, user.id)
assert fetched.name == "Test"
🔍 Interviewer Notes: Transaction rollback pattern is orders of magnitude faster than dropping/recreating tables per test.
❌ Common Mistakes:
- Mocking the session (misses real query bugs and constraint violations)
- Using
scope="function"for engine (recreates DB per test — very slow) - Not rolling back after each test (test pollution between cases)
Q61: How do you use Session.execute() vs Session.scalars() vs Session.scalar()?
Technical Answer:
session.execute(stmt) returns a CursorResult — full rows as Row tuples. session.scalars(stmt) returns a ScalarResult — just the first column of each row (useful when selecting a single model). session.scalar(stmt) returns the first column of the first row as a single value or None.
Code Example:
with Session(engine) as session:
# execute() — returns Row objects (all columns)
rows = session.execute(select(User.id, User.name)).all()
for row in rows:
print(row.id, row.name)
# scalars() — returns first column only (use when selecting a full model)
users = session.scalars(select(User)).all()
for user in users:
print(user.name) # user is a User instance
# scalar() — single value
count = session.scalar(select(func.count(User.id)))
print(count) # integer
# scalar() on a model query — first result or None
user = session.scalar(select(User).where(User.email == "alice@example.com"))
🔍 Interviewer Notes: Choosing the wrong method causes type confusion. scalars() on a multi-column select silently discards non-first columns.
❌ Common Mistakes:
- Using
session.execute(select(User)).all()— returnsRowobjects wrappingUser, notUserdirectly - Using
session.scalars()when selecting multiple columns (drops all but first) - Using
session.scalar()when expecting multiple rows (silently ignores all but first)
Q62: How do you use no_autoflush context manager?
Technical Answer:
session.no_autoflush suspends automatic flushing within the block. Useful when you temporarily need to query during object construction without triggering premature flushes that would fail constraints.
Code Example:
with Session(engine) as session:
# Problem: adding object A requires querying for object B,
# but A is pending and not yet valid — autoflush would fail
new_user = User(name="Alice")
session.add(new_user)
with session.no_autoflush:
# Safe to query here without flushing the pending new_user
existing = session.scalar(
select(User).where(User.name == "Alice")
)
if existing:
raise ValueError("User already exists")
session.commit()
🔍 Interviewer Notes: A niche but important tool for @validates and relationship event handlers that need to query mid-construction.
❌ Common Mistakes:
- Not knowing
no_autoflushexists and disablingautoflushglobally on the session (affects all queries) - Using
no_autoflushas a performance hack (correct fix is usually query optimization) - Forgetting the context manager form —
session.autoflush = Falsewithout restoring it
Q63: How do you use relationship with viewonly=True?
Technical Answer:
viewonly=True creates a read-only relationship. SQLAlchemy won't use it for cascade operations, and changes to the collection won't be persisted. Useful for convenience accessors that use non-standard join conditions.
Code Example:
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
all_posts: Mapped[List["Post"]] = relationship(
back_populates="author"
)
# View-only: recent posts only — NOT used for cascade/writes
recent_posts: Mapped[List["Post"]] = relationship(
primaryjoin="and_(Post.author_id == User.id, Post.created_at > func.now() - text(\"interval '30 days'\"))",
viewonly=True,
lazy="dynamic",
)
with Session(engine) as session:
user = session.get(User, 1)
# Access read-only — no writes through this relationship
recent = user.recent_posts.all()
🔍 Interviewer Notes: viewonly=True prevents accidental writes through computed/filtered relationships.
❌ Common Mistakes:
- Trying to
appendto aviewonlyrelationship (silently ignored — not an error in some versions) - Not using
viewonly=Trueon relationships with customprimaryjoin(can cause unexpected cascade behavior) - Over-using
viewonlywhen a simple query method would be cleaner
Q64: How do you implement a Mixin for common model fields?
Technical Answer:
A Mixin class adds reusable columns/methods to multiple models without inheritance. Common patterns: TimestampMixin (created_at/updated_at), UUIDMixin (public_id), SoftDeleteMixin.
Code Example:
from datetime import datetime
from sqlalchemy.orm import declared_attr
import uuid as _uuid
class TimestampMixin:
created_at: Mapped[datetime] = mapped_column(
DateTime, default=datetime.utcnow, nullable=False
)
updated_at: Mapped[Optional[datetime]] = mapped_column(
DateTime, default=None, onupdate=datetime.utcnow
)
class UUIDMixin:
public_id: Mapped[_uuid.UUID] = mapped_column(
UUIDType(binary=False), default=_uuid.uuid4, unique=True, index=True
)
class AuditMixin:
@declared_attr
def __tablename__(cls):
return cls.__name__.lower() + "s"
created_by: Mapped[Optional[int]] = mapped_column(
ForeignKey("users.id"), nullable=True
)
class Post(Base, TimestampMixin, UUIDMixin):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
# inherits: created_at, updated_at, public_id
🔍 Interviewer Notes: @declared_attr is needed when a mixin defines relationships or attributes that reference cls. Tests ORM design patterns.
❌ Common Mistakes:
- Not using
@declared_attron mixin relationships (causes mapper errors) - Sharing a mutable default like
default=[]for an array column across instances - Forgetting
onupdate=datetime.utcnowonupdated_at(stays as initial value forever)
Q65: How does SQLAlchemy handle expire_on_commit?
Technical Answer:
When expire_on_commit=True (default), all persistent objects are expired after session.commit(). Accessing any attribute after commit triggers a lazy reload from the database. Set to False to avoid reloads, but you risk serving stale data.
Code Example:
# Default behavior (expire_on_commit=True)
with Session(engine) as session:
user = session.get(User, 1)
user.name = "Alice"
session.commit()
# Accessing user.name now triggers a SELECT (re-fetch from DB)
print(user.name) # fresh from DB
# expire_on_commit=False — be careful
SessionNoExpire = sessionmaker(engine, expire_on_commit=False)
with SessionNoExpire() as session:
user = session.get(User, 1)
session.commit()
print(user.name) # uses cached Python value — may be stale!
# Best practice: call refresh() when you need fresh data after commit
with Session(engine) as session:
user = session.get(User, 1)
session.commit()
session.refresh(user) # explicit reload
print(user.name)
🔍 Interviewer Notes: Expiry after commit is the most common cause of unexpected SELECT statements in ORM code. Tests deep session lifecycle understanding.
❌ Common Mistakes:
- Setting
expire_on_commit=Falseglobally to avoid extra queries, then reading stale data - Not knowing that
session.refresh(obj)forces an immediate reload - Accessing many attributes after commit, each triggering a separate SELECT
Q66: How do you use bindparam() for reusable parameterized queries?
Technical Answer:
bindparam() creates a named parameter placeholder reusable across executions. Combined with text() or Core expressions, it enables compiled queries that can be executed multiple times with different values efficiently.
Code Example:
from sqlalchemy import bindparam, text, select
# With text()
stmt = text("SELECT * FROM users WHERE age > :min_age AND status = :status")
stmt = stmt.bindparams(
bindparam("min_age", type_=Integer),
bindparam("status", type_=String),
)
with engine.connect() as conn:
result = conn.execute(stmt, {"min_age": 18, "status": "active"})
# With Core expressions
from sqlalchemy import update
update_stmt = (
update(User)
.where(User.id == bindparam("user_id"))
.values(status=bindparam("new_status"))
)
with Session(engine) as session:
session.execute(update_stmt, [
{"user_id": 1, "new_status": "inactive"},
{"user_id": 2, "new_status": "banned"},
])
session.commit()
🔍 Interviewer Notes: bindparam() with executemany is key for efficient bulk updates with per-row values.
❌ Common Mistakes:
- Using f-strings to inject values instead of bind params (SQL injection risk)
- Not specifying
type_=on bind params when SQLAlchemy needs to know the type - Confusing
bindparam("x")(for execution-time binding) with literal values
Q67: How do you use func for database functions?
Technical Answer:
sqlalchemy.func is a proxy that generates SQL function calls. Any attribute access on func creates a Function construct — func.now(), func.coalesce(), func.upper(), func.concat(), etc.
Code Example:
from sqlalchemy import func, select
with Session(engine) as session:
# String functions
stmt = select(func.upper(User.name), func.length(User.email))
# Date functions
stmt = select(User).where(
func.date_part("year", User.created_at) == 2024
)
# Coalesce (NULL handling)
stmt = select(
User.name,
func.coalesce(User.display_name, User.name).label("shown_name")
)
# Custom/DB-specific functions
stmt = select(func.now()) # current timestamp
stmt = select(func.gen_random_uuid()) # PostgreSQL UUID
# Aggregate with FILTER (PostgreSQL)
stmt = select(
func.count(User.id).filter(User.is_active == True).label("active"),
func.count(User.id).filter(User.is_active == False).label("inactive"),
)
row = session.execute(stmt).one()
🔍 Interviewer Notes: func is SQLAlchemy's escape hatch for any DB function. The .filter() aggregate pattern avoids multiple CASE WHEN statements.
❌ Common Mistakes:
- Not knowing
func.<anything>works — writingtext("now()")instead offunc.now() - Forgetting
.label()on function expressions when accessing by name - Calling
func.count()without arguments whenfunc.count(column)is needed for non-null counts
Q68: How do you handle optimistic locking with version_id_col?
Technical Answer:
Optimistic locking detects concurrent modifications by tracking a version counter. SQLAlchemy's __mapper_args__ with version_id_col automatically increments the version on each UPDATE and raises StaleDataError if versions conflict.
Code Example:
from sqlalchemy import Integer
from sqlalchemy.orm import Mapped, mapped_column
class Account(Base):
__tablename__ = "accounts"
__mapper_args__ = {"version_id_col": "version"}
id: Mapped[int] = mapped_column(primary_key=True)
balance: Mapped[float] = mapped_column(Numeric(12, 2))
version: Mapped[int] = mapped_column(Integer, default=1)
# Conflict detection
with Session(engine) as session1:
acct = session1.get(Account, 1)
with Session(engine) as session2:
acct2 = session2.get(Account, 1)
acct2.balance -= 100
session2.commit() # version incremented to 2
with session1:
acct.balance -= 50
try:
session1.commit() # raises StaleDataError — version mismatch
except StaleDataError:
session1.rollback()
print("Conflict! Re-fetch and retry.")
🔍 Interviewer Notes: Optimistic locking vs pessimistic (SELECT FOR UPDATE) is a classic concurrency interview topic. Optimistic is preferred for low-contention scenarios.
❌ Common Mistakes:
- Not handling
StaleDataError(app crashes on conflict instead of retrying) - Using optimistic locking for high-contention scenarios (too many retries — use pessimistic instead)
- Forgetting to include
versioncolumn in the Alembic migration
Q69: How do you use insert().on_conflict_do_update() for upserts (PostgreSQL)?
Technical Answer:
PostgreSQL's INSERT ... ON CONFLICT DO UPDATE (upsert) inserts a row or updates it if a conflict occurs on a unique constraint. SQLAlchemy exposes this via the insert() construct's .on_conflict_do_update() method.
Code Example:
from sqlalchemy.dialects.postgresql import insert
with Session(engine) as session:
stmt = insert(User).values(
id=1,
email="alice@example.com",
name="Alice",
login_count=1,
)
stmt = stmt.on_conflict_do_update(
index_elements=["email"], # conflict target
set_={
"name": stmt.excluded.name, # use the incoming value
"login_count": User.login_count + 1, # increment existing
"updated_at": func.now(),
}
)
session.execute(stmt)
session.commit()
# Do nothing on conflict
stmt = insert(Tag).values(name="python").on_conflict_do_nothing(
index_elements=["name"]
)
session.execute(stmt)
session.commit()
🔍 Interviewer Notes: Upsert is essential for idempotent data pipelines. Tests PostgreSQL dialect knowledge.
❌ Common Mistakes:
- Using
session.merge()for concurrent upserts (race condition — not atomic) - Importing
insertfromsqlalchemyinstead ofsqlalchemy.dialects.postgresql(noon_conflict_do_update) - Not specifying
index_elementsorconstraint(required conflict target)
Q70: How do you implement full-text search with TSVectorType from sqlalchemy_utils?
Technical Answer:
TSVectorType from sqlalchemy_utils wraps PostgreSQL's tsvector type and integrates with SQLAlchemy's ORM. Combine with a GIN index and to_tsquery for efficient full-text search.
Code Example:
from sqlalchemy_utils import TSVectorType
from sqlalchemy import Index
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
body: Mapped[str] = mapped_column(Text)
search_vector = mapped_column(
TSVectorType("title", "body", weights={"title": "A", "body": "B"})
)
__table_args__ = (
Index("idx_articles_search", "search_vector", postgresql_using="gin"),
)
# Query full-text search
from sqlalchemy import func
with Session(engine) as session:
stmt = (
select(Article)
.where(
Article.search_vector.match("sqlalchemy tutorial")
)
.order_by(
func.ts_rank(Article.search_vector, func.to_tsquery("sqlalchemy & tutorial")).desc()
)
)
results = session.scalars(stmt).all()
🔍 Interviewer Notes: PostgreSQL full-text search is often preferred over Elasticsearch for simpler cases. Tests PostgreSQL + SQLAlchemy extension knowledge.
❌ Common Mistakes:
- Not adding a GIN index on the
tsvectorcolumn (full table scan on every search) - Using
LIKE '%query%'instead of full-text search on large text columns - Not keeping the
search_vectorupdated via trigger or SQLAlchemy event whentitle/bodychange
Q71: How do you use ChoiceType from sqlalchemy_utils?
Technical Answer:
ChoiceType stores a value as a string in the DB but exposes it as a named choice in Python using a (value, label) tuple list. Simpler than Python's enum.Enum for small fixed-choice columns.
Code Example:
from sqlalchemy_utils import ChoiceType
TICKET_STATUS = [
("open", "Open"),
("in_progress","In Progress"),
("resolved", "Resolved"),
("closed", "Closed"),
]
class Ticket(Base):
__tablename__ = "tickets"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
status = mapped_column(ChoiceType(TICKET_STATUS), default="open")
with Session(engine) as session:
t = Ticket(title="Bug fix", status="in_progress")
session.add(t)
session.commit()
ticket = session.get(Ticket, t.id)
print(ticket.status.code) # "in_progress"
print(ticket.status.value) # "In Progress"
🔍 Interviewer Notes: ChoiceType is lighter than Enum for simple display-label mappings.
❌ Common Mistakes:
- Accessing
.valueexpecting the code string (it returns the human-readable label) - Not providing a default value (status is
Noneif unset) - Using
ChoiceTypefor frequently changing choice lists (preferEnumtable or lookup table)
Q72: How do you use ArrowType from sqlalchemy_utils?
Technical Answer:
ArrowType stores datetimes in the DB as UTC but returns them as Arrow objects (from the arrow library), which provide timezone-aware, chainable datetime manipulation.
Code Example:
# pip install arrow sqlalchemy-utils
from sqlalchemy_utils import ArrowType
import arrow
class Event(Base):
__tablename__ = "events"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(200))
starts_at = mapped_column(ArrowType)
ends_at = mapped_column(ArrowType)
with Session(engine) as session:
event = Event(
name="Conference",
starts_at=arrow.utcnow().shift(days=7),
ends_at=arrow.utcnow().shift(days=9),
)
session.add(event)
session.commit()
ev = session.get(Event, event.id)
print(ev.starts_at.humanize()) # "in 7 days"
print(ev.starts_at.to("US/Eastern")) # timezone conversion
print(ev.starts_at.format("YYYY-MM-DD")) # "2024-06-15"
🔍 Interviewer Notes: ArrowType is convenient for human-readable datetime operations. Tests awareness of timezone handling best practices.
❌ Common Mistakes:
- Storing naive datetimes and converting at display time (inconsistent timezone handling)
- Not knowing
ArrowTypealways stores UTC and converts on read - Using
ArrowTypewhenDateTime(timezone=True)with Python'sdatetimewould suffice
Q73: How do you use PhoneNumberType from sqlalchemy_utils?
Technical Answer:
PhoneNumberType stores phone numbers as strings but validates format using the phonenumbers library. It can normalize numbers to E.164 format and validate by region.
Code Example:
# pip install phonenumbers sqlalchemy-utils
from sqlalchemy_utils import PhoneNumberType
class Contact(Base):
__tablename__ = "contacts"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
phone = mapped_column(
PhoneNumberType(region="US", max_length=20),
nullable=True
)
with Session(engine) as session:
c = Contact(name="Alice", phone="+1-555-867-5309")
session.add(c)
session.commit()
contact = session.get(Contact, c.id)
print(contact.phone.e164) # "+15558675309"
print(contact.phone.national) # "(555) 867-5309"
print(contact.phone.is_valid_number) # True
🔍 Interviewer Notes: Phone number normalization at the ORM layer prevents duplicate entries from different formats.
❌ Common Mistakes:
- Storing phone numbers as raw user input ("+1 (555) 867-5309" vs "+15558675309" — duplicates)
- Not setting
region(parsing ambiguous short numbers without a default region fails) - Comparing stored phone numbers as strings without normalizing first
Q74: How do you use ScalarListType from sqlalchemy_utils?
Technical Answer:
ScalarListType stores a Python list as a comma-separated string in the database. It's a portable alternative to ARRAY that works across all databases, not just PostgreSQL.
Code Example:
from sqlalchemy_utils import ScalarListType
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
roles = mapped_column(ScalarListType(str), default=list)
scores = mapped_column(ScalarListType(int), default=list)
with Session(engine) as session:
user = User(name="Alice", roles=["admin", "editor"], scores=[95, 87, 92])
session.add(user)
session.commit()
u = session.get(User, user.id)
print(u.roles) # ["admin", "editor"] — Python list
print(u.scores) # [95, 87, 92]
# Stored in DB as: "admin,editor" and "95,87,92"
🔍 Interviewer Notes: ScalarListType trades query-ability for portability. Not suitable for large lists or lists needing DB-level filtering.
❌ Common Mistakes:
- Storing values containing commas (breaks the serialization — use
ARRAYor JSON instead) - Trying to filter by list contents at the SQL level (no SQL array operators available)
- Using
ScalarListTypefor large lists (better to normalize to a related table)
Q75: What is force_auto_coercion() and force_instant_defaults() in sqlalchemy_utils?
Technical Answer:
force_auto_coercion() enables automatic type coercion for sqlalchemy_utils types (e.g., converting a plain string to PhoneNumber on assignment). force_instant_defaults() evaluates column defaults immediately on object creation instead of at flush time.
Code Example:
from sqlalchemy_utils import force_auto_coercion, force_instant_defaults
# Call once at app startup (before models are defined or imported)
force_auto_coercion() # auto-coerce types on assignment
force_instant_defaults() # evaluate defaults immediately
# Without force_auto_coercion:
user = User(phone="+15558675309") # raises TypeError — expects PhoneNumber
# With force_auto_coercion:
user = User(phone="+15558675309") # auto-converts string to PhoneNumber ✅
# Without force_instant_defaults:
user = User()
print(user.public_id) # None — UUID not yet generated
# With force_instant_defaults:
user = User()
print(user.public_id) # UUID value — generated immediately ✅
🔍 Interviewer Notes: Both helpers reduce friction in tests and serialization. force_instant_defaults() is particularly useful for Pydantic integration.
❌ Common Mistakes:
- Calling them after model imports (too late — must be called first)
- Not knowing these exist and writing manual coercion in every setter
- Using
force_instant_defaults()in production without testing that it doesn't conflict withserver_defaultcolumns
Q76: How do you inspect ORM instance state using inspect()?
Technical Answer:
sqlalchemy.inspect(instance) on an ORM object returns an InstanceState. This gives access to loaded attributes, pending changes, relationship loading status, and the four object states.
Code Example:
from sqlalchemy import inspect as sa_inspect
with Session(engine) as session:
user = User(name="Alice")
state = sa_inspect(user)
print(state.transient) # True (not yet added to session)
print(state.pending) # False
session.add(user)
print(state.pending) # True
print(state.persistent) # False
session.flush()
print(state.persistent) # True
print(state.identity) # (1,) — the PK tuple
# Check loaded/unloaded attributes
print(state.attrs.name.loaded_value) # "Alice"
print(state.attrs.posts.loaded_value) # LoadedValue.NO_VALUE (not loaded)
# Detect dirty attributes
user.name = "Alice Smith"
print(state.attrs.name.history) # History(added=['Alice Smith'], deleted=['Alice'], unchanged=[])
🔍 Interviewer Notes: Instance state inspection is invaluable for debugging and building audit systems. Tests deep ORM knowledge.
❌ Common Mistakes:
- Importing
inspectfrom Python stdlib instead of SQLAlchemy - Not knowing
state.attrs.<col>.historyfor change detection - Checking
state.persistentto decide whether toadd()— usesession.merge()instead
Q77: How do you use has_index() and has_unique_index() from sqlalchemy_utils?
Technical Answer:
has_index(column) and has_unique_index(column) check whether a column has an index or unique index at the database level. Useful in migration scripts and admin tooling to validate schema state.
Code Example:
from sqlalchemy_utils import has_index, has_unique_index
from sqlalchemy import inspect
engine = create_engine("postgresql://user:pass@localhost/mydb")
# Check by column object
from myapp.models import User
insp = inspect(engine)
# has_index checks if ANY index covers the column
print(has_index(User.__table__.c.email)) # True or False
# has_unique_index checks for a UNIQUE index
print(has_unique_index(User.__table__.c.email)) # True or False
# Useful in migration guards
def safe_add_unique_index(conn, table, column_name):
col = table.c[column_name]
if not has_unique_index(col):
conn.execute(
text(f"CREATE UNIQUE INDEX ON {table.name}({column_name})")
)
print(f"Created unique index on {table.name}.{column_name}")
else:
print("Index already exists — skipping")
🔍 Interviewer Notes: Useful for idempotent migration scripts that must run safely on any environment.
❌ Common Mistakes:
- Not using these helpers and writing raw
information_schemaqueries - Checking index existence in application startup (slow — do it in migrations)
- Assuming
unique=Trueon a column always creates an index (it does, but this helper confirms it)
Q78: How do you use naturally_equivalent() from sqlalchemy_utils?
Technical Answer:
naturally_equivalent(a, b) compares two ORM objects by their natural keys (non-PK identifying attributes) rather than by Python identity or PK. Useful when you have detached or transient objects that represent the same logical entity.
Code Example:
from sqlalchemy_utils import naturally_equivalent
class Country(Base):
__tablename__ = "countries"
id: Mapped[int] = mapped_column(primary_key=True)
code: Mapped[str] = mapped_column(String(2), unique=True)
name: Mapped[str] = mapped_column(String(100))
c1 = Country(code="US", name="United States")
c2 = Country(code="US", name="United States")
print(c1 == c2) # False — different Python objects
print(naturally_equivalent(c1, c2)) # True — same natural keys
print(naturally_equivalent(c1, Country(code="GB", name="UK"))) # False
🔍 Interviewer Notes: Useful in tests and data import logic where object identity is unreliable.
❌ Common Mistakes:
- Using
==for logical equality on detached ORM objects (compares Python identity) - Not knowing
naturally_equivalent()and writing custom comparison methods - Using it on objects with no natural key (falls back to PK comparison)
Q79: How do you use make_url() and URL manipulation in SQLAlchemy?
Technical Answer:
make_url() parses a database URL string into a URL object with accessible components. Useful for dynamically building connection strings, masking credentials in logs, and switching databases.
Code Example:
from sqlalchemy.engine import make_url
url = make_url("postgresql+psycopg2://user:secret@localhost:5432/mydb")
print(url.drivername) # "postgresql+psycopg2"
print(url.username) # "user"
print(url.password) # "secret"
print(url.host) # "localhost"
print(url.port) # 5432
print(url.database) # "mydb"
# Safe logging (masks password)
print(url.render_as_string(hide_password=True))
# "postgresql+psycopg2://user:***@localhost:5432/mydb"
# Build URL programmatically
from sqlalchemy.engine.url import URL
dynamic_url = URL.create(
drivername="postgresql+psycopg2",
username="user",
password="secret",
host="localhost",
port=5432,
database="mydb",
)
engine = create_engine(dynamic_url)
🔍 Interviewer Notes: Always use hide_password=True when logging connection strings. Tests awareness of credential hygiene.
❌ Common Mistakes:
- Logging the full URL with password in plaintext (credential leak in logs)
- String-formatting the URL with f-strings (special characters in passwords break the URL)
- Not using
URL.create()when building URLs dynamically (special character escaping issues)
Q80: How do you configure pool_pre_ping, pool_recycle, and connection health checks?
Technical Answer:
pool_pre_ping=True sends a lightweight ping before returning a connection from the pool, detecting stale connections. pool_recycle forces connections to be recycled after N seconds, preventing issues with database-side connection timeouts (e.g., MySQL's default 8-hour timeout).
Code Example:
from sqlalchemy import create_engine, event
from sqlalchemy.pool import Pool
engine = create_engine(
"postgresql+psycopg2://user:pass@localhost/mydb",
pool_size=10,
max_overflow=5,
pool_timeout=30, # wait max 30s for a connection from pool
pool_recycle=1800, # recycle connections after 30 min
pool_pre_ping=True, # test connection health before use
)
# Pool event for monitoring
@event.listens_for(Pool, "checkout")
def on_checkout(dbapi_conn, conn_record, conn_proxy):
pass # connection checked out from pool
@event.listens_for(Pool, "checkin")
def on_checkin(dbapi_conn, conn_record):
pass # connection returned to pool
# For serverless (Lambda, Cloud Functions) — no persistent pool
from sqlalchemy.pool import NullPool
serverless_engine = create_engine(DATABASE_URL, poolclass=NullPool)
🔍 Interviewer Notes: pool_pre_ping=True is essential for any long-running service. NullPool is essential for serverless functions that don't have persistent processes.
❌ Common Mistakes:
- Not setting
pool_pre_ping=True(stale connection errors after DB restart) - Not using
NullPoolin AWS Lambda (connection exhaustion — each invocation opens a new connection) - Setting
pool_recycleshorter than DBwait_timeout(unnecessary churn)
Q81: How do you handle DetachedInstanceError?
Technical Answer:
DetachedInstanceError occurs when you access a lazy-loaded attribute on an object whose session is closed. Fix by: eager-loading in the original query, keeping the session open, calling session.refresh(), or using expire_on_commit=False carefully.
Code Example:
# Problem
with Session(engine) as session:
user = session.get(User, 1)
# session is now closed — user is detached
try:
print(user.posts) # DetachedInstanceError!
except DetachedInstanceError:
print("Session closed — can't lazy load")
# Fix 1: eager load before session closes
with Session(engine) as session:
user = session.scalars(
select(User).options(selectinload(User.posts)).where(User.id == 1)
).one()
# user.posts is already loaded — no lazy load needed
# Fix 2: keep session open (FastAPI pattern)
def get_user(session: Session, user_id: int):
return session.scalars(
select(User).options(selectinload(User.posts)).where(User.id == user_id)
).one()
# Fix 3: refresh in new session
with Session(engine) as session:
session.add(user) # re-attach
session.refresh(user) # reload all attributes
print(user.posts) # works now
🔍 Interviewer Notes: One of the most common SQLAlchemy bugs in web apps. Tests production debugging experience.
❌ Common Mistakes:
- Setting
lazy="raise"on all relationships (catches the problem but doesn't fix it) - Returning ORM objects from endpoints after session closes without eager loading
- Re-using
expire_on_commit=Falseas a global fix (hides the root cause)
Q82: How do you use relationship with lazy="dynamic" vs lazy="select" vs lazy="raise"?
Technical Answer:
lazy="select" (default) loads the collection in a separate SELECT on first access. lazy="dynamic" (deprecated in 2.0, replaced by write_only) returns a query object instead of a list. lazy="raise" raises an error on access — forces explicit loading decisions.
Code Example:
from sqlalchemy.orm import relationship
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
# Default — loads all at once on access
posts_lazy: Mapped[List["Post"]] = relationship(lazy="select")
# raise — fails loudly if not eagerly loaded (forces explicit loading)
posts_raise: Mapped[List["Post"]] = relationship(lazy="raise")
# write_only (2.0 replacement for dynamic)
# — never loads, only supports add()/remove() and explicit queries
from sqlalchemy.orm import WriteOnlyMapped
posts_write_only: WriteOnlyMapped[List["Post"]] = relationship()
# write_only usage
with Session(engine) as session:
user = session.get(User, 1)
# user.posts_write_only # raises — must query explicitly
stmt = select(Post).where(Post.author_id == 1).limit(10)
recent = session.scalars(stmt).all()
🔍 Interviewer Notes: lazy="raise" is recommended in large codebases to catch N+1 issues at development time.
❌ Common Mistakes:
- Still using
lazy="dynamic"in SQLAlchemy 2.0 (deprecated — useWriteOnlyMapped) - Not knowing
lazy="raise"exists as an N+1 detection tool - Setting
lazy="joined"on all relationships globally (increases every query's cost)
Q83: How do you use EncryptedType from sqlalchemy_utils?
Technical Answer:
EncryptedType transparently encrypts values before storing and decrypts on read. Requires a secret key and an encryption backend (AesEngine, FernetEngine). Used for PII, tokens, or sensitive config values.
Code Example:
# pip install cryptography sqlalchemy-utils
from sqlalchemy_utils import EncryptedType
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
import os
SECRET_KEY = os.environ["DB_ENCRYPTION_KEY"] # 32-byte key
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String(255))
ssn = mapped_column(
EncryptedType(String, SECRET_KEY, AesEngine, "pkcs5"),
nullable=True
)
api_token = mapped_column(
EncryptedType(String, SECRET_KEY, AesEngine, "pkcs5"),
nullable=True
)
with Session(engine) as session:
user = User(email="alice@example.com", ssn="123-45-6789")
session.add(user)
session.commit()
u = session.get(User, user.id)
print(u.ssn) # "123-45-6789" — decrypted automatically
# In DB: stored as encrypted ciphertext
🔍 Interviewer Notes: Application-level encryption complements DB encryption. Tests PII handling awareness.
❌ Common Mistakes:
- Storing the encryption key in the codebase (use environment variables or secrets manager)
- Encrypting columns you need to search/filter on (can't do
WHERE ssn = ?on ciphertext) - Not rotating keys (encrypted data is permanently lost if key is lost)
Q84: How do you implement single-table inheritance in SQLAlchemy?
Technical Answer:
Single-table inheritance (STI) stores all subclass rows in one table with a discriminator column. All subclass columns are in the same table; subclass-specific columns are NULL for other types.
Code Example:
class Vehicle(Base):
__tablename__ = "vehicles"
__mapper_args__ = {
"polymorphic_on": "vehicle_type",
"polymorphic_identity": "vehicle",
}
id: Mapped[int] = mapped_column(primary_key=True)
vehicle_type: Mapped[str] = mapped_column(String(50))
make: Mapped[str] = mapped_column(String(100))
model: Mapped[str] = mapped_column(String(100))
# Car-specific (NULL for trucks/bikes)
doors: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
# Truck-specific (NULL for cars/bikes)
payload_tons: Mapped[Optional[float]] = mapped_column(Numeric(6,2), nullable=True)
class Car(Vehicle):
__mapper_args__ = {"polymorphic_identity": "car"}
class Truck(Vehicle):
__mapper_args__ = {"polymorphic_identity": "truck"}
with Session(engine) as session:
session.add_all([
Car(make="Toyota", model="Camry", doors=4),
Truck(make="Ford", model="F-150", payload_tons=1.5),
])
session.commit()
# Query all vehicles
all_v = session.scalars(select(Vehicle)).all()
# Query only cars
cars = session.scalars(select(Car)).all()
🔍 Interviewer Notes: STI is simple but wastes space with NULL columns. Good for shallow hierarchies with few subclass-specific fields.
❌ Common Mistakes:
- Using STI for deep hierarchies with many subclass-specific columns (too many NULLs)
- Forgetting
polymorphic_onon the parent (no discriminator — can't distinguish types) - Querying
Vehicleand expecting only parent-type rows (returns all subclasses too)
Q85: How do you implement joined-table inheritance?
Technical Answer: Joined-table inheritance stores each class in its own table. A query for a subclass automatically JOINs the parent table. Better than STI when subclasses have many unique columns.
Code Example:
class Employee(Base):
__tablename__ = "employees"
__mapper_args__ = {
"polymorphic_on": "emp_type",
"polymorphic_identity": "employee",
}
id: Mapped[int] = mapped_column(primary_key=True)
emp_type: Mapped[str] = mapped_column(String(50))
name: Mapped[str] = mapped_column(String(100))
salary: Mapped[float] = mapped_column(Numeric(10,2))
class Engineer(Employee):
__tablename__ = "engineers"
__mapper_args__ = {"polymorphic_identity": "engineer"}
id: Mapped[int] = mapped_column(ForeignKey("employees.id"), primary_key=True)
tech_stack: Mapped[str] = mapped_column(String(200))
certifications: Mapped[int] = mapped_column(Integer, default=0)
class Manager(Employee):
__tablename__ = "managers"
__mapper_args__ = {"polymorphic_identity": "manager"}
id: Mapped[int] = mapped_column(ForeignKey("employees.id"), primary_key=True)
reports_count: Mapped[int] = mapped_column(Integer, default=0)
budget: Mapped[float] = mapped_column(Numeric(12,2), default=0)
with Session(engine) as session:
eng = Engineer(name="Alice", salary=120000, tech_stack="Python,PostgreSQL")
session.add(eng)
session.commit()
# SELECT employees JOIN engineers WHERE engineers.id = 1
alice = session.get(Engineer, eng.id)
print(alice.tech_stack)
🔍 Interviewer Notes: JTI adds a JOIN to every query but avoids NULL columns. Tests ability to model domain hierarchies properly.
❌ Common Mistakes:
- Defining the subclass
idwithoutForeignKeyto parent (breaks the join) - Using JTI for very shallow hierarchies (the JOIN overhead isn't worth it — use STI)
- Querying the parent class with
with_polymorphic("*")on deep hierarchies (expensive multi-table JOIN)
Q86: How do you use with_polymorphic() to load all subclass data in one query?
Technical Answer:
with_polymorphic(Base, [SubA, SubB]) tells SQLAlchemy to load all specified subclass data in a single query (using OUTER JOINs or UNION). Without it, subclass attributes trigger lazy loads.
Code Example:
from sqlalchemy.orm import with_polymorphic
# Load all employee types in one query
AllEmployees = with_polymorphic(Employee, [Engineer, Manager])
with Session(engine) as session:
stmt = select(AllEmployees).where(AllEmployees.salary > 100000)
employees = session.scalars(stmt).all()
for emp in employees:
if isinstance(emp, Engineer):
print(f"Engineer {emp.name}: {emp.tech_stack}")
elif isinstance(emp, Manager):
print(f"Manager {emp.name}: {emp.reports_count} reports")
# Or configure on the model (always join)
class Employee(Base):
__mapper_args__ = {
"polymorphic_on": "emp_type",
"with_polymorphic": "*", # always join all subclasses
}
🔍 Interviewer Notes: Without with_polymorphic, accessing subclass attributes on a parent query triggers N+1 per subclass type.
❌ Common Mistakes:
- Using
"*"wildcard on models with many subclasses and large tables (expensive) - Not knowing
with_polymorphicexists and getting N+1 on polymorphic queries - Mixing STI and JTI in the same hierarchy (unsupported)
Q87: How do you use selectin_polymorphic() for lazy polymorphic loading?
Technical Answer:
selectin_polymorphic() loads subclass data using a separate SELECT ... WHERE id IN (...) per subclass type, rather than JOINs. More efficient than with_polymorphic when only some rows are each subtype.
Code Example:
from sqlalchemy.orm import selectin_polymorphic
with Session(engine) as session:
# Load employees, then fetch subclass data in separate IN queries
stmt = (
select(Employee)
.options(selectin_polymorphic(Employee, [Engineer, Manager]))
.where(Employee.salary > 80000)
)
employees = session.scalars(stmt).all()
# Generates:
# SELECT employees WHERE salary > 80000
# SELECT engineers WHERE id IN (1, 3, 5, ...)
# SELECT managers WHERE id IN (2, 4, ...)
for emp in employees:
if isinstance(emp, Engineer):
print(emp.tech_stack) # no extra query
🔍 Interviewer Notes: selectin_polymorphic vs with_polymorphic — understanding the trade-off shows senior-level ORM knowledge.
❌ Common Mistakes:
- Using
with_polymorphic("*")whenselectin_polymorphicwould be more efficient - Forgetting that subclass attributes still aren't available without one of these strategies
- Not using either strategy and accepting N+1 on polymorphic queries
Q88: How do you write custom TypeDecorator for reusable column types?
Technical Answer:
TypeDecorator wraps an existing SQLAlchemy type and adds custom Python-to-DB conversion logic. Use it for custom serialization (JSON, encrypted values, comma-separated lists) as a reusable type class.
Code Example:
from sqlalchemy.types import TypeDecorator, TEXT
import json
class JSONEncodedList(TypeDecorator):
"""Stores a Python list as a JSON string in a TEXT column."""
impl = TEXT
cache_ok = True
def process_bind_param(self, value, dialect):
"""Python → DB: called on INSERT/UPDATE"""
if value is not None:
return json.dumps(value)
return value
def process_result_value(self, value, dialect):
"""DB → Python: called on SELECT"""
if value is not None:
return json.loads(value)
return []
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(200))
features = mapped_column(JSONEncodedList, default=list)
with Session(engine) as session:
p = Product(name="Widget", features=["fast", "reliable", "cheap"])
session.add(p)
session.commit()
product = session.get(Product, p.id)
print(product.features) # ["fast", "reliable", "cheap"]
🔍 Interviewer Notes: TypeDecorator is the right abstraction for reusable custom serialization. cache_ok = True is required in SQLAlchemy 1.4+ to avoid warnings.
❌ Common Mistakes:
- Forgetting
cache_ok = True(SQLAlchemy 1.4+ emits aSAWarning) - Not handling
Noneinprocess_bind_param(NULLs causejson.dumps(None)to store"null") - Writing conversion logic inline in every model instead of as a
TypeDecorator
Q89: How do you debug slow queries with SQLAlchemy?
Technical Answer:
Enable echo=True on the engine, use query profiling events, or integrate with a profiler. For production, use EXPLAIN ANALYZE via text() and monitor with slow query logs.
Code Example:
import time
from sqlalchemy import event
# 1. Engine-level logging
engine = create_engine(DATABASE_URL, echo=True) # logs all SQL
# 2. Timing via events
@event.listens_for(engine, "before_cursor_execute")
def before_execute(conn, cursor, statement, params, context, executemany):
context._query_start = time.perf_counter()
@event.listens_for(engine, "after_cursor_execute")
def after_execute(conn, cursor, statement, params, context, executemany):
elapsed = time.perf_counter() - context._query_start
if elapsed > 0.1: # log queries slower than 100ms
import logging
logging.warning(f"SLOW QUERY ({elapsed:.3f}s):\n{statement}")
# 3. EXPLAIN ANALYZE (PostgreSQL)
with engine.connect() as conn:
plan = conn.execute(
text("EXPLAIN ANALYZE SELECT * FROM users WHERE email = :e"),
{"e": "alice@example.com"}
).fetchall()
for row in plan:
print(row[0])
# 4. Count queries in a block (detect N+1)
query_count = {"n": 0}
@event.listens_for(engine, "after_cursor_execute")
def count_queries(conn, cursor, statement, params, context, executemany):
query_count["n"] += 1
🔍 Interviewer Notes: Query counting in tests is a practical N+1 detection technique. Tests production debugging maturity.
❌ Common Mistakes:
- Using
echo=Truein production (logs credentials and sensitive data) - Not using
EXPLAIN ANALYZE(justEXPLAIN— doesn't show actual execution time) - Counting queries in tests without asserting a maximum (no automatic regression detection)
Q90: How do you implement database seeding with SQLAlchemy?
Technical Answer:
Seeding populates the database with initial or test data. Use session.execute(insert(...)) for bulk seeds, or a dedicated seed.py script that runs after migrations. Always make seeds idempotent.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
def seed_roles(session: Session):
"""Idempotent role seeding."""
roles = [
{"id": 1, "name": "admin", "description": "Full access"},
{"id": 2, "name": "editor", "description": "Content access"},
{"id": 3, "name": "viewer", "description": "Read-only"},
]
stmt = pg_insert(Role).values(roles)
stmt = stmt.on_conflict_do_nothing(index_elements=["id"])
session.execute(stmt)
def seed_all():
engine = create_engine(DATABASE_URL)
with Session(engine) as session:
seed_roles(session)
session.commit()
print("Seeding complete")
if __name__ == "__main__":
seed_all()
🔍 Interviewer Notes: Idempotent seeds (safe to run multiple times) are essential. Tests awareness of ON CONFLICT DO NOTHING.
❌ Common Mistakes:
- Non-idempotent seeds that fail on re-run (using
session.add_all()without conflict handling) - Seeding in application startup code (slow boot time, runs on every process start)
- Hard-coding IDs for seed data that conflict with sequences in PostgreSQL
Q91: How do you use RETURNING clause in SQLAlchemy?
Technical Answer:
.returning() adds a RETURNING clause (PostgreSQL/SQLite 3.35+/MariaDB 10.5+). It returns values from the affected rows without a second query — useful for getting generated IDs, timestamps, or confirming updates.
Code Example:
from sqlalchemy import insert, update, delete
with Session(engine) as session:
# Get generated values on INSERT
result = session.execute(
insert(User)
.values(name="Alice", email="alice@example.com")
.returning(User.id, User.created_at)
)
row = result.fetchone()
print(f"Created user id={row.id} at {row.created_at}")
# Get updated values
result = session.execute(
update(User)
.where(User.id == 1)
.values(login_count=User.login_count + 1)
.returning(User.login_count)
)
new_count = result.scalar()
# Bulk insert with returning
result = session.execute(
insert(Tag).returning(Tag.id, Tag.name),
[{"name": "python"}, {"name": "django"}, {"name": "fastapi"}]
)
created_tags = result.fetchall()
session.commit()
🔍 Interviewer Notes: RETURNING eliminates the SELECT-after-INSERT/UPDATE pattern. Not supported on MySQL (use lastrowid instead).
❌ Common Mistakes:
- Using
RETURNINGon MySQL (not supported — raisesCompileError) - Not calling
session.commit()after the execute (values returned but not persisted) - Fetching
returning()results after commit (cursor is closed — fetch before commit)
Q92: How do you handle schema-per-tenant multi-tenancy in SQLAlchemy?
Technical Answer:
Schema-per-tenant isolates each tenant in their own PostgreSQL schema. Use __table_args__ = {"schema": schema_name} or set the search path per connection using a connection event.
Code Example:
from sqlalchemy import event, text
def set_search_path(connection, tenant_schema: str):
"""Set PostgreSQL search_path to tenant schema."""
connection.execute(text(f"SET search_path TO {tenant_schema}, public"))
# Per-request schema switching (FastAPI example)
@contextmanager
def tenant_session(tenant_id: str):
schema = f"tenant_{tenant_id}"
with engine.connect() as conn:
set_search_path(conn, schema)
with Session(bind=conn) as session:
yield session
# Or configure per-engine
def get_tenant_engine(tenant_id: str):
schema = f"tenant_{tenant_id}"
eng = engine.execution_options(
schema_translate_map={None: schema}
)
return eng
# Usage
with tenant_session("acme") as session:
users = session.scalars(select(User)).all()
# queries run against tenant_acme.users
🔍 Interviewer Notes: Schema-per-tenant vs row-level-security vs separate DB is a system design question. Tests architectural knowledge.
❌ Common Mistakes:
- Not sanitizing
tenant_idbefore using inSET search_path(SQL injection via schema name) - Not creating the schema in migrations when onboarding a new tenant
- Using schema switching with a connection pool without resetting search_path (tenant bleed)
Q93: How do you use Session.get_bind() for read replica routing?
Technical Answer:
Override Session.get_bind() to route read queries to a replica and write queries to the primary. This is a lightweight way to implement read/write splitting without an external proxy.
Code Example:
from sqlalchemy.orm import Session
primary_engine = create_engine(PRIMARY_URL)
replica_engine = create_engine(REPLICA_URL)
class RoutingSession(Session):
def get_bind(self, mapper=None, clause=None, **kwargs):
# Route SELECT to replica, everything else to primary
from sqlalchemy.sql import Select
if clause is not None and isinstance(clause, Select):
return replica_engine
return primary_engine
RoutingSessionFactory = sessionmaker(class_=RoutingSession)
with RoutingSessionFactory() as session:
# SELECT — goes to replica
users = session.scalars(select(User)).all()
# INSERT/UPDATE/DELETE — goes to primary
session.add(User(name="New User"))
session.commit()
🔍 Interviewer Notes: Read replica routing is a senior-level architecture pattern. Tests ability to extend SQLAlchemy's core mechanisms.
❌ Common Mistakes:
- Routing writes to the replica (data loss — replica is usually read-only)
- Not handling replication lag (reading your own writes may miss them on the replica)
- Using this pattern without understanding that the session manages a single transaction (writes and reads are still in separate connections)
Q94: How do you use declarative_mixin with declared_attr for relationships in mixins?
Technical Answer:
When a mixin defines a relationship(), use @declared_attr so it's evaluated per-class rather than shared. Without it, all classes share one relationship descriptor, causing mapper errors.
Code Example:
from sqlalchemy.orm import declared_attr, relationship
class HasCommentsMixin:
@declared_attr
def comments(cls) -> Mapped[List["Comment"]]:
return relationship(
"Comment",
primaryjoin=f"Comment.entity_id == {cls.__name__}.id",
foreign_keys="Comment.entity_id",
viewonly=True,
)
@declared_attr
def comment_count(cls):
from sqlalchemy.orm import column_property
from sqlalchemy import select, func
return column_property(
select(func.count(Comment.id))
.where(Comment.entity_id == cls.id)
.correlate_except(Comment)
.scalar_subquery()
)
class Post(Base, HasCommentsMixin):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
class Article(Base, HasCommentsMixin):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
headline: Mapped[str] = mapped_column(String(200))
🔍 Interviewer Notes: @declared_attr is the key to reusable relationship mixins. One of the most missed SQLAlchemy features.
❌ Common Mistakes:
- Defining
relationship()directly in the mixin without@declared_attr(shared across all classes — mapper error) - Not using
cls.__name__inprimaryjoin(hardcoded class name breaks reuse) - Forgetting that
@declared_attrmethods must return the column/relationship, not assign it
Q95: How do you profile memory usage with SQLAlchemy in large queries?
Technical Answer:
For large result sets, use yield_per() to fetch in batches rather than loading all rows into memory. For introspection, use tracemalloc or objgraph to detect ORM object accumulation.
Code Example:
from sqlalchemy import select
import tracemalloc
# yield_per — process rows in chunks without loading all into memory
with Session(engine) as session:
stmt = select(User).order_by(User.id).execution_options(yield_per=500)
for partition in session.scalars(stmt).partitions(500):
for user in partition:
process(user) # process 500 at a time
session.expunge_all() # free memory after each batch
# Memory profiling
tracemalloc.start()
with Session(engine) as session:
users = session.scalars(select(User)).all()
snapshot = tracemalloc.take_snapshot()
top = snapshot.statistics("lineno")[:5]
for stat in top:
print(stat)
🔍 Interviewer Notes: yield_per is critical for ETL jobs and data exports. Tests awareness of ORM memory footprint.
❌ Common Mistakes:
- Loading 1M+ rows with
.all()(OOM crash) - Not calling
session.expunge_all()between batches (identity map grows unboundedly) - Using
yield_perwith eager loading (breaks batching — eager load fetches all at once)
Q96: How do you use Inspector.get_unique_constraints() and get_check_constraints()?
Technical Answer:
Inspector.get_unique_constraints(table) returns named unique constraints (not just unique indexes). get_check_constraints(table) returns check constraint definitions including their SQL expression strings.
Code Example:
from sqlalchemy import inspect
insp = inspect(engine)
# Unique constraints
unique_constraints = insp.get_unique_constraints("users")
for uc in unique_constraints:
print(f"Unique: {uc['name']} on {uc['column_names']}")
# Unique: uq_users_email on ['email']
# Unique: uq_user_dept on ['user_id', 'department_id']
# Check constraints
check_constraints = insp.get_check_constraints("orders")
for cc in check_constraints:
print(f"Check: {cc['name']}: {cc['sqltext']}")
# Check: chk_positive_amount: amount > 0
# Check: chk_order_status: status IN ('pending', 'confirmed', 'cancelled')
# Sorted tables with FK dependencies (useful for ordered creation/deletion)
sorted_tables = insp.get_sorted_table_and_fkc_names()
for table_name, fk_collection in sorted_tables:
print(table_name)
🔍 Interviewer Notes: Used in schema validation tooling, migration scripts, and admin panels that need to display schema metadata.
❌ Common Mistakes:
- Confusing unique constraints with unique indexes (
get_indexes()vsget_unique_constraints()— both can enforce uniqueness but are different objects) - Not knowing
get_sorted_table_and_fkc_names()for dependency-ordered operations - Using reflection on every request (cache
Inspectorresults at startup)
Q97: How does SQLAlchemy 2.0 differ from 1.x in session usage?
Technical Answer:
SQLAlchemy 2.0 introduced "2.0-style" execution everywhere: session.execute(select(...)) replaces session.query(...). engine.execute() is removed. Context managers are mandatory for Session and Connection. Mapped[] annotations replace Column().
Code Example:
# SQLAlchemy 1.x (Legacy)
session = Session(engine)
users = session.query(User).filter(User.age > 18).all()
session.close() # easy to forget
# SQLAlchemy 2.0 (Modern)
with Session(engine) as session:
users = session.scalars(
select(User).where(User.age > 18)
).all()
# session auto-closed
# Model definition 1.x
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String(100))
# Model definition 2.0
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# Execution 1.x (removed in 2.0)
# engine.execute("SELECT 1") # REMOVED
# Execution 2.0
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
🔍 Interviewer Notes: Candidates maintaining existing codebases must know the 1.x→2.0 differences. Tests framework version awareness.
❌ Common Mistakes:
- Using
session.query()in 2.0 (still works for compatibility but is the legacy style) - Not using context managers (
with Session()) — session stays open, connections leak - Using
engine.execute()which was removed in 2.0
Q98: How do you use Mapped and mapped_column() with complex types?
Technical Answer:
Mapped[T] provides full Python type annotation for ORM columns. Optional[T] marks nullable columns. List["Model"] marks one-to-many relationships. mapped_column() accepts all Column() parameters.
Code Example:
from typing import Optional, List
from datetime import datetime
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy import String, Text, Numeric, DateTime, Boolean
class Article(Base):
__tablename__ = "articles"
# Not nullable (Mapped[str], not Optional)
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
slug: Mapped[str] = mapped_column(String(200), unique=True)
# Nullable (Optional)
subtitle: Mapped[Optional[str]] = mapped_column(String(300), default=None)
published_at: Mapped[Optional[datetime]] = mapped_column(DateTime, default=None)
# With defaults
view_count: Mapped[int] = mapped_column(default=0)
is_featured: Mapped[bool] = mapped_column(Boolean, default=False)
price: Mapped[Optional[float]] = mapped_column(Numeric(10, 2), default=None)
# Relationships
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
author: Mapped["User"] = relationship(back_populates="articles")
tags: Mapped[List["Tag"]] = relationship(secondary=article_tags)
🔍 Interviewer Notes: Proper Mapped usage enables mypy/pyright type checking on ORM models. Optional vs non-Optional maps to nullable.
❌ Common Mistakes:
- Using
Mapped[str]for a nullable column (should beMapped[Optional[str]]) - Forgetting that
Mapped[int]with nodefaultand nonullablemeans the column is required at insert - Using old
Column()syntax insidemapped_column()wrapper (they conflict)
Q99: What is MappedAsDataclass and when should you use it?
Technical Answer:
MappedAsDataclass makes SQLAlchemy models behave like Python dataclasses — they get auto-generated __init__, __repr__, __eq__ based on field definitions. Available in SQLAlchemy 2.0+.
Code Example:
from sqlalchemy.orm import MappedAsDataclass, DeclarativeBase, Mapped, mapped_column
from dataclasses import field
from typing import Optional
class Base(MappedAsDataclass, DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True, init=False)
name: Mapped[str] = mapped_column(String(100))
email: Mapped[str] = mapped_column(String(255), unique=True)
age: Mapped[Optional[int]] = mapped_column(default=None)
is_active: Mapped[bool] = mapped_column(default=True)
# Auto-generated __init__
user = User(name="Alice", email="alice@example.com")
print(user) # User(id=None, name='Alice', email='alice@example.com', age=None, is_active=True)
# Keyword-only (no positional confusion)
user2 = User(name="Bob", email="bob@example.com", age=30)
🔍 Interviewer Notes: MappedAsDataclass reduces boilerplate and integrates better with typing tools and Pydantic. A 2.0-specific feature.
❌ Common Mistakes:
- Mixing
MappedAsDataclasswith manual__init__(conflict) - Forgetting
init=Falseonid(would require passingid=to every constructor) - Using
MappedAsDataclasson models with complex mixin relationships (ordering offield()matters)
Q100: How do you implement a generic repository pattern with SQLAlchemy?
Technical Answer:
The Repository pattern wraps session access behind a typed interface, decoupling the domain layer from SQLAlchemy. Use generics (TypeVar) so one base repository handles all models.
Code Example:
from typing import TypeVar, Generic, Type, Optional, List
from sqlalchemy.orm import Session
from sqlalchemy import select
ModelT = TypeVar("ModelT")
class Repository(Generic[ModelT]):
def __init__(self, model: Type[ModelT], session: Session):
self.model = model
self.session = session
def get(self, id: int) -> Optional[ModelT]:
return self.session.get(self.model, id)
def get_all(self, limit: int = 100, offset: int = 0) -> List[ModelT]:
return self.session.scalars(
select(self.model).limit(limit).offset(offset)
).all()
def create(self, **kwargs) -> ModelT:
obj = self.model(**kwargs)
self.session.add(obj)
self.session.flush()
return obj
def delete(self, id: int) -> bool:
obj = self.get(id)
if obj:
self.session.delete(obj)
return True
return False
class UserRepository(Repository[User]):
def get_by_email(self, email: str) -> Optional[User]:
return self.session.scalar(
select(User).where(User.email == email)
)
# Usage
with Session(engine) as session:
repo = UserRepository(User, session)
user = repo.create(name="Alice", email="alice@example.com")
session.commit()
found = repo.get_by_email("alice@example.com")
🔍 Interviewer Notes: Repository pattern is a common architecture question. Tests knowledge of generics, typing, and separation of concerns.
❌ Common Mistakes:
- Making the repository own the session (should be injected — enables transaction spanning multiple repos)
- Not using generics (separate
UserRepository,PostRepositorywith duplicate code) - Over-engineering with a full Unit of Work when a simple
Sessionalready provides it
2. Intermediate Topics
Q101: How does SQLAlchemy's connection pool work internally?
Technical Answer:
The pool maintains a set of DBAPI connections. When code requests a connection, the pool checks out one from the idle queue; when done, it returns it. QueuePool is the default — it has pool_size (persistent connections) and max_overflow (temporary burst connections).
Code Example:
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool, NullPool, StaticPool
# QueuePool (default) — for web apps
engine = create_engine(
DATABASE_URL,
poolclass=QueuePool,
pool_size=5, # persistent idle connections
max_overflow=10, # extra connections allowed during bursts
pool_timeout=30, # seconds to wait for a connection
pool_recycle=1800, # recycle connections older than 30 min
pool_pre_ping=True, # health-check before checkout
)
# NullPool — for AWS Lambda, scripts, one-off tasks
engine_lambda = create_engine(DATABASE_URL, poolclass=NullPool)
# StaticPool — for in-memory SQLite in tests
engine_test = create_engine(
"sqlite:///:memory:",
poolclass=StaticPool,
connect_args={"check_same_thread": False},
)
# Monitor pool status
from sqlalchemy import event
from sqlalchemy.pool import Pool
@event.listens_for(Pool, "connect")
def on_connect(dbapi_conn, conn_record):
print("New connection created")
@event.listens_for(Pool, "checkout")
def on_checkout(dbapi_conn, conn_record, conn_proxy):
print(f"Connection checked out. Pool size: {engine.pool.size()}")
🔍 Interviewer Notes: Pool configuration is one of the first production tuning topics. Tests DevOps/infrastructure awareness.
❌ Common Mistakes:
- Setting
pool_sizetoo large (connection pressure on DB side) - Not using
NullPoolin serverless functions (connections accumulate per invocation) - Not monitoring pool overflow (indicates DB is saturated)
Q102: How do you configure SQLAlchemy for async usage?
Technical Answer:
SQLAlchemy 1.4+ supports async via create_async_engine and AsyncSession. Requires an async DBAPI driver (e.g., asyncpg for PostgreSQL, aiosqlite for SQLite). All session operations become awaitable.
Code Example:
# pip install sqlalchemy[asyncio] asyncpg
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy import select
async_engine = create_async_engine(
"postgresql+asyncpg://user:pass@localhost/mydb",
pool_size=10,
echo=False,
)
AsyncSessionLocal = async_sessionmaker(
async_engine,
expire_on_commit=False,
class_=AsyncSession,
)
# FastAPI async dependency
async def get_db():
async with AsyncSessionLocal() as session:
yield session
# Usage
async def get_user(session: AsyncSession, user_id: int):
return await session.get(User, user_id)
async def list_users(session: AsyncSession):
result = await session.execute(select(User).where(User.is_active == True))
return result.scalars().all()
# Async context manager
async def main():
async with AsyncSessionLocal() as session:
async with session.begin():
user = User(name="Alice", email="alice@example.com")
session.add(user)
# committed
🔍 Interviewer Notes: Async SQLAlchemy is now standard in FastAPI/Starlette apps. Tests modern async Python knowledge.
❌ Common Mistakes:
- Using sync
create_enginewithasyncpg(driver mismatch — crashes) - Calling
session.scalars()withoutawaitin async context - Using
expire_on_commit=True(default) with async — triggers unexpected lazy loads after commit that requireawait
Q103: How do you run sync SQLAlchemy code from an async context?
Technical Answer:
Use AsyncSession.run_sync() to run synchronous code that uses the session's underlying synchronous connection. This is useful when migrating legacy sync code to an async codebase incrementally.
Code Example:
from sqlalchemy.ext.asyncio import AsyncSession
async def do_something_sync(session: AsyncSession):
def sync_operation(sync_session):
# Runs in async loop but uses sync session interface
users = sync_session.scalars(select(User)).all()
return users
users = await session.run_sync(sync_operation)
return users
# Also for DDL operations (create_all is sync only)
async def create_tables(engine):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def drop_tables(engine):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
🔍 Interviewer Notes: run_sync() bridges legacy sync and new async code. Tests async migration knowledge.
❌ Common Mistakes:
- Running blocking I/O inside
run_sync()(blocks the event loop) - Using
Base.metadata.create_all(engine)directly on an async engine (raises error — must userun_sync) - Not knowing
run_sync()exists and wrapping inasyncio.to_thread()unnecessarily
Q104: How do you handle Alembic migrations with SQLAlchemy models?
Technical Answer:
Alembic reads your Base.metadata to detect schema changes and generates migration scripts. Key workflow: init Alembic, configure env.py to point to your models, run alembic revision --autogenerate, then alembic upgrade head.
Code Example:
# Initialize
alembic init alembic
# alembic/env.py — point to your models
from myapp.database import Base
target_metadata = Base.metadata
# alembic.ini
# sqlalchemy.url = postgresql://user:pass@localhost/mydb
# alembic/env.py (full async version)
from sqlalchemy.ext.asyncio import create_async_engine
from alembic import context
def run_migrations_online():
connectable = create_async_engine(DATABASE_URL)
async def run_async_migrations():
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
asyncio.run(run_async_migrations())
# Generated migration
def upgrade() -> None:
op.add_column("users", sa.Column("bio", sa.Text(), nullable=True))
op.create_index("idx_users_email", "users", ["email"], unique=True)
def downgrade() -> None:
op.drop_index("idx_users_email", table_name="users")
op.drop_column("users", "bio")
🔍 Interviewer Notes: Alembic autogenerate misses some changes (partial indexes, CHECK constraints in some dialects, sequences). Always review generated migrations.
❌ Common Mistakes:
- Running
create_all()in production instead of Alembic (no rollback, no history) - Not reviewing autogenerated migrations (Alembic can miss constraints or generate wrong DROP statements)
- Not setting
compare_type=Trueinenv.py(column type changes not detected by default)
Q105: How do you write a data migration in Alembic?
Technical Answer:
Data migrations transform existing data alongside schema changes. Use op.execute() for raw SQL, or get a bound connection via op.get_bind() to run ORM-style operations.
Code Example:
# alembic/versions/xxxx_populate_slug.py
from alembic import op
import sqlalchemy as sa
from sqlalchemy.orm import Session
import re
def slugify(text: str) -> str:
return re.sub(r"[^a-z0-9-]", "-", text.lower()).strip("-")
def upgrade() -> None:
# 1. Add nullable column first
op.add_column("articles", sa.Column("slug", sa.String(200), nullable=True))
# 2. Populate existing rows
bind = op.get_bind()
session = Session(bind=bind)
articles = session.execute(sa.text("SELECT id, title FROM articles")).fetchall()
for article in articles:
slug = slugify(article.title)
session.execute(
sa.text("UPDATE articles SET slug = :slug WHERE id = :id"),
{"slug": slug, "id": article.id}
)
session.commit()
# 3. Now make it NOT NULL and UNIQUE
op.alter_column("articles", "slug", nullable=False)
op.create_unique_constraint("uq_articles_slug", "articles", ["slug"])
def downgrade() -> None:
op.drop_constraint("uq_articles_slug", "articles")
op.drop_column("articles", "slug")
🔍 Interviewer Notes: The 3-step pattern (add nullable → populate → constrain) is the standard approach for non-destructive migrations.
❌ Common Mistakes:
- Adding
NOT NULLcolumn without a default to a table with existing rows (constraint violation) - Running data migration in a single transaction on a large table (locks the table)
- Not writing a
downgrade()(breaks rollback)
Q106: How does SQLAlchemy handle autoflush and when should you disable it?
Technical Answer:
autoflush=True (default) flushes pending changes before every query so the query sees the latest state. Disable it (autoflush=False or session.no_autoflush) when building objects that temporarily violate constraints, or in @validates hooks that query the session.
Code Example:
# autoflush=True (default) — flush before every query
with Session(engine) as session:
user = User(name="Alice")
session.add(user)
# autoflush fires here — inserts Alice before SELECT
count = session.scalar(select(func.count(User.id)))
# Disable globally (use carefully)
NoFlushSession = sessionmaker(engine, autoflush=False)
with NoFlushSession() as session:
user = User(name="Bob")
session.add(user)
# Bob is NOT in the DB yet — count doesn't include him
count = session.scalar(select(func.count(User.id)))
session.flush() # explicit flush
session.commit()
# Disable temporarily with context manager
with Session(engine) as session:
with session.no_autoflush:
# Complex multi-step construction that temporarily breaks constraints
dept = Department(name="Engineering")
emp = Employee(name="Alice")
emp.department = dept # FK not yet valid
session.add(dept)
session.add(emp)
# Autoflush resumes here
session.commit()
🔍 Interviewer Notes: Unexpected autoflush causes N+1 issues and premature constraint violations. Tests deep session lifecycle knowledge.
❌ Common Mistakes:
- Disabling
autoflushglobally and forgetting to flush before queries (reading stale state) - Not knowing
autoflushfires inside@validates(can cause recursive flush) - Relying on
autoflushin tests without realizing it makes tests DB-hit heavy
Q107: How do you use Session.identity_map to understand object caching?
Technical Answer:
The identity map caches loaded objects by their primary key. When you session.get(User, 1) twice, the second call returns the cached Python object without hitting the DB. This is the "first-level cache."
Code Example:
with Session(engine) as session:
# First get — hits DB
user1 = session.get(User, 1)
# Second get — returns same Python object from identity map (NO DB query)
user2 = session.get(User, 1)
print(user1 is user2) # True — same object!
# Inspect the identity map
from sqlalchemy import inspect
imap = inspect(session).identity_map
print(dict(imap)) # {(User, (1,)): <User id=1>}
# Force fresh data — expire the object first
session.expire(user1)
user3 = session.get(User, 1) # hits DB (user1 was expired)
# Clear entire identity map
session.expire_all()
# or
session.expunge_all() # removes from session (makes detached)
🔍 Interviewer Notes: Identity map is the reason session.get() is efficient. Tests understanding of when SQLAlchemy does or doesn't hit the DB.
❌ Common Mistakes:
- Calling
session.get()in a loop expecting fresh DB data each time (gets cached object) - Not knowing that
session.commit()expires all objects (next access re-fetches) - Confusing
session.expire()(marks stale, re-fetches on access) withsession.expunge()(detaches)
Q108: How do you use relationship with a custom primaryjoin?
Technical Answer:
primaryjoin overrides the default JOIN condition for a relationship. Required when the join condition is complex (non-FK columns, expressions, or polymorphic conditions).
Code Example:
from sqlalchemy.orm import relationship
from sqlalchemy import and_
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# Only active posts
active_posts: Mapped[List["Post"]] = relationship(
"Post",
primaryjoin="and_(Post.author_id == User.id, Post.is_deleted == False)",
viewonly=True,
)
# Posts from last 30 days (expression-based)
recent_posts: Mapped[List["Post"]] = relationship(
"Post",
primaryjoin="and_(Post.author_id == User.id, "
"Post.created_at >= func.now() - text(\"interval '30 days'\"))",
viewonly=True,
foreign_keys="[Post.author_id]",
)
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
is_deleted: Mapped[bool] = mapped_column(default=False)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
🔍 Interviewer Notes: Custom primaryjoin combined with viewonly=True is the standard pattern for filtered relationship views.
❌ Common Mistakes:
- Not setting
viewonly=Trueon filtered relationships (SQLAlchemy tries to persist via the relationship) - Not specifying
foreign_keys=whenprimaryjoinis ambiguous (mapper can't determine the FK side) - Using non-string
primaryjoinwith forward references (causesNameErrorat class definition time)
Q109: How do you use lazy="write_only" (SQLAlchemy 2.0)?
Technical Answer:
WriteOnlyMapped (lazy="write_only") replaces the deprecated lazy="dynamic". It provides a collection that never loads into memory — you can append/remove elements and execute explicit queries against it, but never iterate it directly.
Code Example:
from sqlalchemy.orm import WriteOnlyMapped, relationship
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# write_only — suitable for large collections (millions of posts)
posts: WriteOnlyMapped[List["Post"]] = relationship(back_populates="author")
with Session(engine) as session:
user = session.get(User, 1)
# Add to collection (no load)
new_post = Post(title="New Post")
user.posts.add(new_post)
session.commit()
# Remove from collection
old_post = session.get(Post, 99)
user.posts.remove(old_post)
session.commit()
# Query the collection explicitly
stmt = user.posts.select().where(Post.published == True).limit(10)
recent = session.scalars(stmt).all()
🔍 Interviewer Notes: write_only is essential for parent objects with potentially millions of children (users → notifications, orders → events).
❌ Common Mistakes:
- Still using
lazy="dynamic"in SQLAlchemy 2.0 (deprecated, issuesLegacyAPIWarning) - Iterating a
write_onlyrelationship directly (raisesTypeError) - Using
write_onlyfor small collections where normallazy="select"would be simpler
Q110: How do you use select_related equivalent in SQLAlchemy (joined eager loading)?
Technical Answer:
SQLAlchemy's equivalent to Django's select_related is joinedload(). It uses a SQL JOIN to fetch related objects in a single query. Best for many-to-one and one-to-one relationships.
Code Example:
from sqlalchemy.orm import joinedload, selectinload, contains_eager
with Session(engine) as session:
# joinedload — LEFT OUTER JOIN (best for many-to-one)
posts = session.scalars(
select(Post).options(joinedload(Post.author))
).unique().all()
for post in posts:
print(f"{post.title} by {post.author.name}") # no extra query
# selectinload — separate IN query (best for one-to-many collections)
users = session.scalars(
select(User).options(selectinload(User.posts))
).all()
# contains_eager — when you already JOIN manually
stmt = (
select(Post)
.join(Post.author)
.where(User.is_active == True)
.options(contains_eager(Post.author))
)
posts = session.scalars(stmt).unique().all()
🔍 Interviewer Notes: joinedload vs selectinload choice is a key performance question. contains_eager is for when you control the JOIN.
❌ Common Mistakes:
- Using
joinedloadon one-to-many (duplicates rows — must call.unique()) - Forgetting
.unique()afterjoinedloadon collections (duplicate objects in result) - Using
contains_eagerwithout an explicit.join()in the query (attribute not populated)
Q111: How do you implement row-level security with SQLAlchemy?
Technical Answer: Row-level security (RLS) filters every query to only return rows the current user is authorized to see. Implement via: query filters in repositories, PostgreSQL RLS policies, or SQLAlchemy query events.
Code Example:
from sqlalchemy import event
from sqlalchemy.orm import Session, Query
# Approach 1: Repository-level filtering
class PostRepository:
def __init__(self, session: Session, current_user_id: int):
self.session = session
self.current_user_id = current_user_id
def get_all(self):
return self.session.scalars(
select(Post).where(Post.author_id == self.current_user_id)
).all()
# Approach 2: SQLAlchemy query event (2.0 style via custom Session)
class SecureSession(Session):
def __init__(self, *args, current_user_id=None, **kwargs):
super().__init__(*args, **kwargs)
self.current_user_id = current_user_id
# Approach 3: PostgreSQL RLS (most secure — enforced at DB level)
# In Alembic migration:
# op.execute("ALTER TABLE posts ENABLE ROW LEVEL SECURITY")
# op.execute("""
# CREATE POLICY user_posts ON posts
# USING (author_id = current_setting('app.user_id')::int)
# """)
# Set the session variable per request
with engine.connect() as conn:
conn.execute(text("SET app.user_id = :uid"), {"uid": current_user_id})
# All queries in this connection now filtered by RLS
🔍 Interviewer Notes: DB-level RLS is the most secure (can't be bypassed by ORM), but application-level filtering is more portable. Tests security architecture knowledge.
❌ Common Mistakes:
- Implementing RLS only in the API layer (bypassed by direct DB access or scripts)
- Not setting the RLS session variable before queries (sees no rows or all rows)
- Forgetting to test RLS with a non-admin database role (superusers bypass RLS by default)
Q112: How do you use SQLAlchemy with connection string from environment variables?
Technical Answer:
Never hardcode credentials. Use os.environ, python-dotenv, or a secrets manager to build the connection string. SQLAlchemy's URL.create() handles special characters in passwords safely.
Code Example:
import os
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
# Simple approach
DATABASE_URL = os.environ["DATABASE_URL"]
engine = create_engine(DATABASE_URL)
# Explicit component approach (handles special chars in password)
engine = create_engine(
URL.create(
drivername="postgresql+psycopg2",
username=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"], # special chars handled safely
host=os.environ.get("DB_HOST", "localhost"),
port=int(os.environ.get("DB_PORT", 5432)),
database=os.environ["DB_NAME"],
)
)
# With pydantic-settings (FastAPI pattern)
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
db_user: str
db_password: str
db_host: str = "localhost"
db_port: int = 5432
db_name: str
@property
def database_url(self) -> str:
return f"postgresql+psycopg2://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
class Config:
env_file = ".env"
🔍 Interviewer Notes: URL component construction avoids @ and : in passwords breaking the connection string. Tests security discipline.
❌ Common Mistakes:
f"postgresql://user:{password}@host/db"when password contains@or/(breaks URL parsing)- Committing
.envfiles to git (credential leak) - Not validating DB connection at startup (fails silently until first request)
Q113: How do you implement audit logging with SQLAlchemy events?
Technical Answer:
Use SessionEvents (before_flush) to capture changes to ORM objects and write them to an audit log. Access session.new, session.dirty, and session.deleted to see pending changes before they're committed.
Code Example:
from sqlalchemy import event, Column, Integer, String, JSON, DateTime
from sqlalchemy.orm import Session
class AuditLog(Base):
__tablename__ = "audit_log"
id: Mapped[int] = mapped_column(primary_key=True)
table_name: Mapped[str] = mapped_column(String(100))
operation: Mapped[str] = mapped_column(String(10)) # INSERT/UPDATE/DELETE
record_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
changes: Mapped[dict] = mapped_column(JSON, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
@event.listens_for(Session, "before_flush")
def audit_changes(session, flush_context, instances):
from sqlalchemy import inspect as sa_inspect
for obj in session.new:
session.add(AuditLog(
table_name=obj.__tablename__,
operation="INSERT",
record_id=None, # not yet assigned
changes={col: getattr(obj, col) for col in obj.__table__.columns.keys()},
))
for obj in session.dirty:
state = sa_inspect(obj)
changes = {}
for attr in state.attrs:
hist = attr.history
if hist.has_changes():
changes[attr.key] = {"old": hist.deleted[0] if hist.deleted else None,
"new": hist.added[0] if hist.added else None}
if changes:
session.add(AuditLog(
table_name=obj.__tablename__,
operation="UPDATE",
record_id=getattr(obj, "id", None),
changes=changes,
))
for obj in session.deleted:
session.add(AuditLog(
table_name=obj.__tablename__,
operation="DELETE",
record_id=getattr(obj, "id", None),
changes=None,
))
🔍 Interviewer Notes: Audit logging via session events is the ORM-idiomatic approach. Tests event system fluency.
❌ Common Mistakes:
- Writing audit logs in
after_commit(already committed — can't rollback the data change if audit write fails) - Capturing changes in
after_flushinstead ofbefore_flush(history cleared after flush) - Not handling the case where
session.newobjects don't have their PK yet (useafter_flushfor PK)
Q114: How do you use op.batch_alter_table() in Alembic for SQLite?
Technical Answer:
SQLite has limited ALTER TABLE support (no DROP COLUMN, no ADD CONSTRAINT directly). Alembic's batch_alter_table works around this by recreating the table: copies data, drops old table, renames new one.
Code Example:
# alembic migration — works for SQLite AND other DBs
def upgrade() -> None:
with op.batch_alter_table("users") as batch_op:
batch_op.add_column(sa.Column("phone", sa.String(20), nullable=True))
batch_op.drop_column("old_field")
batch_op.alter_column("name", type_=sa.String(200))
batch_op.create_index("idx_users_phone", ["phone"])
batch_op.create_unique_constraint("uq_users_phone", ["phone"])
def downgrade() -> None:
with op.batch_alter_table("users") as batch_op:
batch_op.drop_constraint("uq_users_phone")
batch_op.drop_index("idx_users_phone")
batch_op.drop_column("phone")
batch_op.add_column(sa.Column("old_field", sa.String(100), nullable=True))
batch_op.alter_column("name", type_=sa.String(100))
🔍 Interviewer Notes: Using batch_alter_table for all migrations ensures portability across databases including SQLite (used in tests).
❌ Common Mistakes:
- Using
op.alter_column()directly on SQLite (fails — not supported) - Not using
batch_alter_tablewhen tests use SQLite but prod uses PostgreSQL - Forgetting that batch mode recreates the entire table (expensive for large tables)
Q115: How do you implement connection retry logic in SQLAlchemy?
Technical Answer:
Use pool_pre_ping=True for automatic stale connection detection. For transient errors (connection reset, DB restart), implement application-level retry logic using tenacity or a custom decorator.
Code Example:
from tenacity import retry, stop_after_attempt, wait_exponential
from sqlalchemy.exc import OperationalError, DisconnectionError
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type((OperationalError, DisconnectionError)),
)
def execute_with_retry(session: Session, stmt):
try:
result = session.execute(stmt)
session.commit()
return result
except (OperationalError, DisconnectionError):
session.rollback()
raise
# Connection-level retry with pre-ping
engine = create_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_recycle=3600,
connect_args={
"connect_timeout": 10,
"keepalives": 1,
"keepalives_idle": 30,
"keepalives_interval": 10,
"keepalives_count": 5,
}
)
🔍 Interviewer Notes: Retry logic + pool_pre_ping covers most transient DB failures in production. Tests operational reliability knowledge.
❌ Common Mistakes:
- Retrying without rollback (session is in a broken state after an exception)
- Retrying on non-transient errors like
IntegrityError(infinite loop) - Not setting TCP keepalive on long-idle connections (connection silently dropped by firewall)
Q116: How does selectinload differ from subqueryload?
Technical Answer:
selectinload uses SELECT ... WHERE id IN (1, 2, 3, ...) — fast and memory-efficient, works with async. subqueryload uses a correlated subquery — was the legacy choice before selectinload. In SQLAlchemy 2.0, selectinload is almost always preferred.
Code Example:
from sqlalchemy.orm import selectinload, subqueryload
with Session(engine) as session:
# selectinload — 2 queries: SELECT users, then SELECT posts WHERE author_id IN (...)
users = session.scalars(
select(User).options(selectinload(User.posts))
).all()
# subqueryload — 2 queries: SELECT users, then SELECT posts with correlated subquery
# Deprecated preference — use selectinload instead
users = session.scalars(
select(User).options(subqueryload(User.posts))
).all()
# Nested eager loading
users = session.scalars(
select(User).options(
selectinload(User.posts).selectinload(Post.comments)
)
).all()
# 3 total queries: users, posts WHERE author_id IN (...), comments WHERE post_id IN (...)
🔍 Interviewer Notes: selectinload is the correct modern choice. subqueryload has memory scaling issues with large result sets.
❌ Common Mistakes:
- Using
subqueryloadin new code (deprecated preference, less efficient) - Nesting
joinedloadinsideselectinload(generates a cross-product JOIN) - Not nesting eager loads — accessing
post.commentsafterselectinload(User.posts)triggers N+1
Q117: How do you use aggregated decorator from sqlalchemy_utils?
Technical Answer:
@aggregated(relationship, column) from sqlalchemy_utils creates a column that auto-updates when the aggregated relationship changes. Useful for denormalized counts or sums stored directly on the parent model.
Code Example:
from sqlalchemy_utils import aggregated
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
comments: Mapped[List["Comment"]] = relationship()
# Auto-updated whenever comments change
@aggregated("comments", mapped_column(Integer, default=0))
def comment_count(self):
from sqlalchemy import func
return func.count("1")
@aggregated("comments", mapped_column(Numeric(10,2), default=0))
def avg_rating(self):
from sqlalchemy import func
return func.avg(Comment.rating)
class Comment(Base):
__tablename__ = "comments"
id: Mapped[int] = mapped_column(primary_key=True)
post_id: Mapped[int] = mapped_column(ForeignKey("posts.id"))
body: Mapped[str] = mapped_column(Text)
rating: Mapped[float] = mapped_column(Numeric(3,1))
# Usage
with Session(engine) as session:
post = session.get(Post, 1)
print(post.comment_count) # automatically maintained
🔍 Interviewer Notes: aggregated avoids expensive COUNT queries on every page load. Tests awareness of denormalization patterns.
❌ Common Mistakes:
- Not creating the aggregated column in the Alembic migration
- Bulk-inserting comments bypassing the ORM (aggregated column won't update)
- Using
aggregatedfor frequently changing values under high write load (too many UPDATE triggers)
Q118: How do you use observes decorator from sqlalchemy_utils?
Technical Answer:
@observes(relationship) triggers a callback when the observed relationship changes. Used for derived/computed fields that depend on relationship data but aren't simple aggregates.
Code Example:
from sqlalchemy_utils import observes
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
orders: Mapped[List["Order"]] = relationship()
is_vip: Mapped[bool] = mapped_column(Boolean, default=False)
total_spent: Mapped[float] = mapped_column(Numeric(10,2), default=0)
@observes("orders")
def order_observer(self, orders):
self.total_spent = sum(o.total for o in orders)
self.is_vip = self.total_spent > 10000
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
total: Mapped[float] = mapped_column(Numeric(10,2))
🔍 Interviewer Notes: observes is useful for complex derived state that can't be expressed as a simple aggregate function.
❌ Common Mistakes:
- Doing expensive operations inside the observer (runs synchronously before flush)
- Relying on
observesfor bulk-loaded data (bypasses ORM events) - Not testing the observer trigger — it only fires when the relationship changes via the ORM
Q119: How do you implement full pagination with cursor-based approach?
Technical Answer:
Cursor-based (keyset) pagination uses a stable sort key from the last seen row instead of OFFSET. It's O(log n) vs O(n) for large offsets and works correctly when rows are inserted/deleted between pages.
Code Example:
from sqlalchemy import select
from typing import Optional
import base64, json
def encode_cursor(id: int, created_at: datetime) -> str:
payload = json.dumps({"id": id, "created_at": created_at.isoformat()})
return base64.b64encode(payload.encode()).decode()
def decode_cursor(cursor: str) -> dict:
payload = base64.b64decode(cursor.encode()).decode()
return json.loads(payload)
def paginate_posts(
session: Session,
limit: int = 20,
after_cursor: Optional[str] = None,
):
stmt = select(Post).order_by(Post.created_at.desc(), Post.id.desc())
if after_cursor:
decoded = decode_cursor(after_cursor)
cursor_date = datetime.fromisoformat(decoded["created_at"])
cursor_id = decoded["id"]
stmt = stmt.where(
(Post.created_at < cursor_date) |
((Post.created_at == cursor_date) & (Post.id < cursor_id))
)
posts = session.scalars(stmt.limit(limit + 1)).all()
has_more = len(posts) > limit
posts = posts[:limit]
next_cursor = None
if has_more and posts:
last = posts[-1]
next_cursor = encode_cursor(last.id, last.created_at)
return {"items": posts, "next_cursor": next_cursor, "has_more": has_more}
🔍 Interviewer Notes: Cursor pagination is the correct approach for feeds, timelines, and high-traffic APIs. Tests algorithmic thinking + SQL knowledge.
❌ Common Mistakes:
- Using
OFFSETfor production pagination (degrades to O(n) on large tables) - Using a non-unique sort column as cursor (duplicate values produce inconsistent pages)
- Not including the
idas a tiebreaker in cursor sort (rows with identicalcreated_atskipped)
Q120: How do you use SQLAlchemy with PostgreSQL-specific features like LISTEN/NOTIFY?
Technical Answer:
PostgreSQL's LISTEN/NOTIFY allows real-time messaging between DB sessions. Use psycopg2 directly (via SQLAlchemy's raw connection) or asyncpg for async listeners.
Code Example:
import asyncio
import asyncpg
# Send notification from SQLAlchemy
with engine.connect() as conn:
conn.execute(text("NOTIFY channel_name, 'payload_data'"))
conn.commit()
# Or via trigger (common pattern)
# CREATE TRIGGER notify_on_insert AFTER INSERT ON orders
# EXECUTE FUNCTION pg_notify('new_order', row_to_json(NEW)::text)
# Listen with asyncpg
async def listen_for_events():
conn = await asyncpg.connect(DATABASE_URL)
await conn.add_listener("new_order", handle_new_order)
await asyncio.sleep(3600) # keep listening
async def handle_new_order(conn, pid, channel, payload):
import json
order_data = json.loads(payload)
print(f"New order received: {order_data}")
# Sync listener with psycopg2
import psycopg2, select as io_select
def sync_listen():
conn = psycopg2.connect(DATABASE_URL)
conn.autocommit = True
cur = conn.cursor()
cur.execute("LISTEN new_order")
while True:
io_select.select([conn], [], [], 5)
conn.poll()
while conn.notifies:
notify = conn.notifies.pop()
print(f"Received: {notify.payload}")
🔍 Interviewer Notes: LISTEN/NOTIFY enables lightweight event-driven architecture without a separate message broker.
❌ Common Mistakes:
- Using a pooled connection for
LISTEN(pool recycles connection — listener is lost) - Not calling
conn.poll()in the sync listener (notifications accumulate but aren't processed) - Sending large payloads via
NOTIFY(limited to 8KB — send IDs and fetch from DB instead)
Q121: How do you handle IntegrityError and UniqueViolation in SQLAlchemy?
Technical Answer:
IntegrityError wraps DB-level constraint violations. Check the orig attribute for the DBAPI-specific exception. Use psycopg2.errors.UniqueViolation (or sqlalchemy.exc.IntegrityError) for pattern matching.
Code Example:
from sqlalchemy.exc import IntegrityError
from psycopg2.errors import UniqueViolation, ForeignKeyViolation
def create_user(session: Session, email: str, name: str) -> User:
try:
user = User(email=email, name=name)
session.add(user)
session.commit()
return user
except IntegrityError as e:
session.rollback()
if isinstance(e.orig, UniqueViolation):
raise ValueError(f"Email {email} already registered") from e
elif isinstance(e.orig, ForeignKeyViolation):
raise ValueError("Referenced record does not exist") from e
raise
# Or check without psycopg2 import
def create_user_portable(session: Session, email: str, name: str):
try:
user = User(email=email, name=name)
session.add(user)
session.commit()
return user
except IntegrityError as e:
session.rollback()
error_msg = str(e.orig).lower()
if "unique" in error_msg or "duplicate" in error_msg:
raise ValueError(f"Duplicate entry: {email}")
raise
🔍 Interviewer Notes: Proper error handling distinguishes unique violations from other constraints. Tests production error handling maturity.
❌ Common Mistakes:
- Not calling
session.rollback()afterIntegrityError(session in broken state) - Catching
Exceptioninstead ofIntegrityError(too broad) - Not re-raising unknown
IntegrityErrorsubtypes (swallows unexpected constraint violations)
Q122: How do you implement optimistic locking with custom version columns?
Technical Answer:
Beyond version_id_col, you can implement optimistic locking manually by including the expected version in the WHERE clause of UPDATE statements and checking affected row counts.
Code Example:
from sqlalchemy import update
from sqlalchemy.exc import StaleDataError
def update_with_version(session: Session, user_id: int, new_name: str, expected_version: int):
result = session.execute(
update(User)
.where(User.id == user_id, User.version == expected_version)
.values(name=new_name, version=User.version + 1)
)
session.commit()
if result.rowcount == 0:
session.rollback()
raise StaleDataError(
f"User {user_id} was modified by another transaction. "
f"Expected version {expected_version}."
)
# With SQLAlchemy's built-in version_id_col
class Document(Base):
__tablename__ = "documents"
__mapper_args__ = {
"version_id_col": version_counter,
"version_id_generator": lambda v: v + 1,
}
id: Mapped[int] = mapped_column(primary_key=True)
content: Mapped[str] = mapped_column(Text)
version_counter: Mapped[int] = mapped_column(Integer, default=1)
🔍 Interviewer Notes: Manual version checking via rowcount == 0 is a portable pattern even without version_id_col.
❌ Common Mistakes:
- Not checking
rowcountafter the update (assumes success even when version mismatch) - Using the same approach in high-contention scenarios (too many retries — use pessimistic locking)
- Not incrementing the version in the UPDATE
VALUES(version stays the same, no protection)
Q123: How do you use op.execute() for complex schema operations in Alembic?
Technical Answer:
op.execute() runs raw SQL in an Alembic migration. Useful for DB-specific operations that Alembic's op.* methods don't cover: creating functions, triggers, views, custom types, or PostgreSQL extensions.
Code Example:
def upgrade() -> None:
# Enable PostgreSQL extension
op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm")
op.execute("CREATE EXTENSION IF NOT EXISTS uuid-ossp")
# Create a database view
op.execute("""
CREATE OR REPLACE VIEW active_user_summary AS
SELECT
u.id,
u.name,
COUNT(p.id) AS post_count,
MAX(p.created_at) AS last_post_at
FROM users u
LEFT JOIN posts p ON p.author_id = u.id
WHERE u.is_active = TRUE
GROUP BY u.id, u.name
""")
# Create a trigger function
op.execute("""
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql
""")
op.execute("""
CREATE TRIGGER trigger_update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION update_updated_at()
""")
def downgrade() -> None:
op.execute("DROP TRIGGER IF EXISTS trigger_update_users_updated_at ON users")
op.execute("DROP FUNCTION IF EXISTS update_updated_at()")
op.execute("DROP VIEW IF EXISTS active_user_summary")
🔍 Interviewer Notes: Raw SQL migrations are needed for triggers, views, and extensions. Tests ability to bridge Alembic with DB-native features.
❌ Common Mistakes:
- Not writing the
downgrade()for raw SQL operations (breaks rollback) - Using
IF NOT EXISTSinupgradebut notIF EXISTSindowngrade(migration fails on re-run) - Creating views without
OR REPLACE(fails if view already exists from a partial run)
Q124: How do you handle large result sets with server-side cursors?
Technical Answer:
Server-side cursors stream results from the DB one batch at a time instead of loading all rows into memory. In PostgreSQL, use stream_results=True with yield_per().
Code Example:
from sqlalchemy import select
# Method 1: yield_per with execution options
with Session(engine) as session:
stmt = select(User).order_by(User.id).execution_options(
stream_results=True,
yield_per=1000
)
for partition in session.scalars(stmt).partitions(1000):
for user in partition:
process_user(user)
session.expunge_all() # free memory
# Method 2: Core level with server-side cursor
with engine.connect().execution_options(stream_results=True) as conn:
result = conn.execute(select(User.__table__).order_by(User.__table__.c.id))
while True:
rows = result.fetchmany(1000)
if not rows:
break
for row in rows:
process_row(row)
# Async version
async with AsyncSession(async_engine) as session:
stmt = select(User).execution_options(yield_per=500)
async for partition in await session.stream_scalars(stmt):
for user in partition:
await process_user_async(user)
🔍 Interviewer Notes: Critical for ETL pipelines, data exports, and batch jobs. Tests production database handling.
❌ Common Mistakes:
- Using
.all()on millions of rows (OOM crash) - Not calling
session.expunge_all()between batches (identity map fills memory) - Using server-side cursors inside a short-lived connection (cursor closed before streaming completes)
Q125: How do you implement the Unit of Work pattern explicitly?
Technical Answer:
SQLAlchemy's Session implements Unit of Work internally. You can make it explicit by creating a dedicated UoW class that manages the session lifecycle and provides typed repository access.
Code Example:
from sqlalchemy.orm import Session
from contextlib import contextmanager
class UnitOfWork:
def __init__(self, session_factory):
self._session_factory = session_factory
def __enter__(self):
self.session = self._session_factory()
self.users = UserRepository(self.session)
self.posts = PostRepository(self.session)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
self.rollback()
self.session.close()
def commit(self):
self.session.commit()
def rollback(self):
self.session.rollback()
# Usage
uow_factory = lambda: UnitOfWork(SessionLocal)
def create_post_with_tags(user_id: int, title: str, tag_names: list):
with UnitOfWork(SessionLocal) as uow:
user = uow.users.get(user_id)
post = Post(title=title, author=user)
for name in tag_names:
tag = uow.posts.get_or_create_tag(name)
post.tags.append(tag)
uow.posts.add(post)
uow.commit()
return post
🔍 Interviewer Notes: UoW is a DDD pattern often asked in architecture interviews. SQLAlchemy Session already implements it — the wrapper just makes it explicit.
❌ Common Mistakes:
- Creating a new
Sessionper repository (breaks transaction atomicity) - Not closing the session in
__exit__(connection leak) - Over-engineering — plain
Sessioncontext manager is sufficient for most apps
Q126: How do you implement soft-delete with global query filtering?
Technical Answer:
Use SQLAlchemy 1.4+ with_loader_criteria() or a custom Session subclass to inject deleted_at IS NULL into every query automatically, preventing accidentally returning deleted records.
Code Example:
from sqlalchemy.orm import with_loader_criteria, Session
from sqlalchemy import event
# Approach: Session-level filtering with events (SQLAlchemy 1.4+)
class SoftDeleteSession(Session):
pass
@event.listens_for(SoftDeleteSession, "do_orm_execute")
def _add_global_filter(execute_state):
if (
execute_state.is_select
and not execute_state.is_column_load
and not execute_state.execution_options.get("include_deleted", False)
):
execute_state.user_defined_options = [
with_loader_criteria(
SoftDeleteMixin,
lambda cls: cls.deleted_at.is_(None),
include_aliases=True,
)
]
# Usage
with SoftDeleteSession(engine) as session:
users = session.scalars(select(User)).all() # auto-filtered
# Override when needed
all_users = session.scalars(
select(User).execution_options(include_deleted=True)
).all()
🔍 Interviewer Notes: Global filters via session events are a clean alternative to remembering to add WHERE deleted_at IS NULL everywhere.
❌ Common Mistakes:
- Not providing an override mechanism (can't query deleted records for admin/audit)
- Applying the filter to non-soft-deletable models (errors on tables without
deleted_at) - Using this approach in async code without testing (event behavior differs with async)
Q127: How do you use text() with TypeDecorator return types?
Technical Answer:
text() results return raw DB values. To get typed results back from raw SQL, use columns() on the text() clause to declare expected column types.
Code Example:
from sqlalchemy import text, Integer, String, DateTime
# Without typing — returns raw tuples
with engine.connect() as conn:
result = conn.execute(text("SELECT id, name, created_at FROM users LIMIT 5"))
rows = result.fetchall()
print(type(rows[0].created_at)) # str or datetime depending on driver
# With column typing — ensures proper Python type conversion
typed_stmt = text("SELECT id, name, created_at FROM users LIMIT 5").columns(
id=Integer(),
name=String(),
created_at=DateTime(),
)
with engine.connect() as conn:
result = conn.execute(typed_stmt)
for row in result:
print(row.id, row.name, row.created_at) # properly typed
# With ORM — map raw SQL to ORM class
with Session(engine) as session:
users = session.scalars(
select(User).from_statement(
text("SELECT * FROM users WHERE age > :age").bindparams(age=18)
)
).all()
🔍 Interviewer Notes: columns() on text() is underused but critical for correct datetime and custom type handling from raw SQL.
❌ Common Mistakes:
- Not typing raw SQL columns (gets strings instead of datetimes)
- Using
session.execute(text(...)).scalars()expecting ORM objects (returns raw values) - Forgetting
.bindparams()and using string formatting instead (SQL injection)
Q128: How do you implement multi-column ordering with nulls-first/last?
Technical Answer:
Use .nulls_first() and .nulls_last() modifiers on asc() and desc() to control where NULLs appear in sorted results. Behavior varies by database (PostgreSQL NULLs-last by default in ASC).
Code Example:
from sqlalchemy import asc, desc, nulls_first, nulls_last
with Session(engine) as session:
# NULLs last in ascending order (PostgreSQL default for ASC)
stmt = select(Employee).order_by(asc(Employee.salary).nulls_last())
# NULLs first in descending order
stmt = select(Employee).order_by(desc(Employee.salary).nulls_first())
# Multi-column ordering
stmt = select(Employee).order_by(
Employee.department.asc(),
nulls_last(Employee.salary.desc()),
Employee.name.asc(),
)
# CASE-based custom ordering
from sqlalchemy import case
priority_order = case(
(Employee.status == "critical", 1),
(Employee.status == "high", 2),
(Employee.status == "normal", 3),
else_=4,
)
stmt = select(Employee).order_by(priority_order, Employee.name)
🔍 Interviewer Notes: NULL ordering behavior differs between databases. Explicit nulls_first()/nulls_last() ensures consistent results.
❌ Common Mistakes:
- Not accounting for NULL values in ORDER BY (position varies by DB)
- Using SQLite in tests (NULL ordering differs from PostgreSQL default behavior)
- Not knowing
case()as a custom sort key for status-based ordering
Q129: How do you use func.coalesce() and func.nullif() effectively?
Technical Answer:
coalesce(a, b, c) returns the first non-NULL value. nullif(a, b) returns NULL if a == b, else returns a. Both are essential for NULL handling in SQL expressions.
Code Example:
from sqlalchemy import func, select
with Session(engine) as session:
# coalesce — fallback chain
stmt = select(
User.id,
func.coalesce(User.display_name, User.username, User.email).label("shown_name")
)
# nullif — treat empty string as NULL
stmt = select(User).where(
func.nullif(User.bio, "").is_not(None)
)
# Aggregate with coalesce (replace NULL count with 0)
stmt = select(
Department.name,
func.coalesce(func.count(Employee.id), 0).label("headcount"),
).outerjoin(Employee, Employee.department_id == Department.id)\
.group_by(Department.name)
# Avoid division by zero with nullif
stmt = select(
func.sum(Revenue.amount) / func.nullif(func.count(Revenue.id), 0)
)
🔍 Interviewer Notes: coalesce and nullif are standard SQL but their SQLAlchemy syntax is often not known. Tests SQL expression fluency.
❌ Common Mistakes:
- Using Python
orinstead offunc.coalesce()(Pythonoris not SQL-level) - Not using
nullif()to avoid division by zero (crashes on empty groups) - Forgetting that
coalesce()short-circuits (expensive expressions in later args still evaluated in some DBs)
Q130: How do you implement optimistic concurrency with a hash-based ETag?
Technical Answer:
ETags can be implemented by hashing the record's content. Compare the incoming If-Match header with the current hash before updating. Use SQLAlchemy @hybrid_property or a before_flush event to compute the hash.
Code Example:
import hashlib, json
from sqlalchemy.orm import hybrid_property
from sqlalchemy import event
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
content: Mapped[str] = mapped_column(Text)
etag: Mapped[str] = mapped_column(String(64), nullable=True)
def compute_etag(self) -> str:
data = json.dumps({"title": self.title, "content": self.content}, sort_keys=True)
return hashlib.sha256(data.encode()).hexdigest()[:16]
@event.listens_for(Article, "before_insert")
@event.listens_for(Article, "before_update")
def update_etag(mapper, connection, target):
target.etag = target.compute_etag()
# FastAPI endpoint
async def update_article(id: int, data: dict, if_match: str, session: AsyncSession):
article = await session.get(Article, id)
if article.etag != if_match:
raise HTTPException(412, "Precondition Failed — article was modified")
article.title = data["title"]
article.content = data["content"]
await session.commit()
return {"etag": article.etag}
🔍 Interviewer Notes: ETag-based concurrency is common in REST APIs. Tests ability to combine HTTP semantics with SQLAlchemy events.
❌ Common Mistakes:
- Computing ETags in the application layer on every response (expensive for large content)
- Not including all mutable fields in the hash (partial updates bypass the check)
- Using a sequential counter as ETag (leaks revision count information)
Q131: How do you implement a soft-delete with Alembic migration?
Technical Answer: Adding a soft-delete column to an existing table requires a careful migration: add nullable column, index it for query performance, and update any existing unique constraints that need to allow "deleted" duplicates.
Code Example:
# alembic migration
def upgrade() -> None:
op.add_column("users", sa.Column(
"deleted_at", sa.DateTime(), nullable=True, server_default=None
))
# Partial index for active records (PostgreSQL)
op.create_index(
"idx_users_active",
"users",
["email"],
unique=True,
postgresql_where="deleted_at IS NULL",
)
# Drop old unique constraint (allows duplicates in soft-deleted rows)
op.drop_constraint("uq_users_email", "users", type_="unique")
def downgrade() -> None:
op.create_unique_constraint("uq_users_email", "users", ["email"])
op.drop_index("idx_users_active", table_name="users")
op.drop_column("users", "deleted_at")
🔍 Interviewer Notes: Partial unique index is the correct solution for soft-delete + unique constraint coexistence.
❌ Common Mistakes:
- Keeping a full unique constraint on
emailafter adding soft-delete (blocks re-registration with same email) - Not indexing
deleted_at(every query that filters deleted records does a full scan) - Not handling existing data in the migration (rows may have values that conflict with new constraints)
Q132: How do you implement a generic soft-delete query filter using __init_subclass__?
Technical Answer:
__init_subclass__ runs when a class inherits from your mixin. Combine with @event.listens_for registered per subclass to automatically add soft-delete filtering to all model queries.
Code Example:
from sqlalchemy import event
from sqlalchemy.orm import Session
class SoftDeleteMixin:
deleted_at: Mapped[Optional[datetime]] = mapped_column(default=None, index=True)
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
@event.listens_for(Session, "do_orm_execute")
def filter_deleted(execute_state):
if execute_state.is_select and not execute_state.execution_options.get("include_deleted"):
execute_state.user_defined_options += [
with_loader_criteria(
cls,
lambda c: c.deleted_at.is_(None),
include_aliases=True,
)
]
class User(Base, SoftDeleteMixin):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
def delete(self):
self.deleted_at = datetime.utcnow()
# Usage
with Session(engine) as session:
user = session.get(User, 1)
user.delete()
session.commit()
# Auto-filtered — won't return deleted users
active = session.scalars(select(User)).all()
# Include deleted when needed
all_users = session.scalars(
select(User).execution_options(include_deleted=True)
).all()
🔍 Interviewer Notes: __init_subclass__ + session events is a clean way to implement cross-cutting concerns for model hierarchies.
❌ Common Mistakes:
- Registering the event listener on every subclass (creates duplicate filters)
- Not providing the
include_deletedescape hatch (can't access soft-deleted data) - Using class-level
__init_subclass__for SQLAlchemy column definitions (usedeclared_attrinstead)
Q133: How do you handle lazy="raise_on_sql" in testing?
Technical Answer:
lazy="raise_on_sql" (or lazy="raise") raises InvalidRequestError when a lazy load would require a SQL query. Used in tests to assert all necessary data was eagerly loaded, catching N+1 before production.
Code Example:
# Model configured for strict loading detection
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
# Use raise in tests, select in production
author: Mapped["User"] = relationship(lazy="raise")
# Test that catches N+1
def test_no_n_plus_one(session):
posts = session.scalars(
select(Post).options(joinedload(Post.author)) # eager load
).unique().all()
for post in posts:
print(post.author.name) # OK — already loaded
def test_raises_without_eager_load(session):
posts = session.scalars(select(Post)).all()
with pytest.raises(InvalidRequestError, match="lazy load"):
_ = posts[0].author # raises — no eager load specified
🔍 Interviewer Notes: lazy="raise" as a testing configuration is a best practice for performance-critical codebases.
❌ Common Mistakes:
- Setting
lazy="raise"in production (breaks any code path that doesn't eagerly load) - Using environment-based config to switch
lazy(complex — useexecution_optionsper query instead) - Not testing with
lazy="raise"and shipping N+1 bugs to production
Q134: How do you use op.rename_table() and op.rename_column() in Alembic?
Technical Answer:
op.rename_table() and op.alter_column(new_column_name=...) rename database objects. Always include downgrade() to reverse the rename.
Code Example:
def upgrade() -> None:
# Rename table
op.rename_table("old_table_name", "new_table_name")
# Rename column
op.alter_column(
"users",
"username",
new_column_name="login",
existing_type=sa.String(100),
existing_nullable=False,
)
# Rename index (PostgreSQL)
op.execute("ALTER INDEX idx_old_name RENAME TO idx_new_name")
# Rename constraint
op.execute(
"ALTER TABLE users RENAME CONSTRAINT uq_old_name TO uq_new_name"
)
def downgrade() -> None:
op.execute("ALTER TABLE users RENAME CONSTRAINT uq_new_name TO uq_old_name")
op.execute("ALTER INDEX idx_new_name RENAME TO idx_old_name")
op.alter_column(
"users",
"login",
new_column_name="username",
existing_type=sa.String(100),
existing_nullable=False,
)
op.rename_table("new_table_name", "old_table_name")
🔍 Interviewer Notes: Autogenerate does NOT detect renames (it sees a drop + add). Must be written manually. Tests awareness of Alembic limitations.
❌ Common Mistakes:
- Relying on autogenerate for renames (generates drop + add, losing data)
- Not specifying
existing_typeinalter_column(required for type-aware DBs) - Forgetting to update FKs, indexes, and constraints that reference the renamed column/table
Q135: How do you handle schema differences between test and production databases?
Technical Answer:
Use the same Alembic migrations for both environments. Never use create_all() in production. For tests, either apply migrations to a dedicated test DB or use create_all() with checkfirst=True against an in-memory DB.
Code Example:
# conftest.py — test database setup
import pytest
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database, drop_database
TEST_DB_URL = "postgresql://user:pass@localhost/test_myapp"
@pytest.fixture(scope="session")
def db_engine():
if not database_exists(TEST_DB_URL):
create_database(TEST_DB_URL)
engine = create_engine(TEST_DB_URL)
# Option A: Apply Alembic migrations (matches production exactly)
from alembic.config import Config
from alembic import command
alembic_cfg = Config("alembic.ini")
alembic_cfg.set_main_option("sqlalchemy.url", TEST_DB_URL)
command.upgrade(alembic_cfg, "head")
yield engine
# Option B: create_all (faster, but misses migration-specific logic)
# Base.metadata.create_all(engine)
drop_database(TEST_DB_URL)
@pytest.fixture
def session(db_engine):
conn = db_engine.connect()
trans = conn.begin()
sess = Session(bind=conn)
yield sess
sess.close()
trans.rollback()
conn.close()
🔍 Interviewer Notes: Applying migrations in tests catches migration bugs before production. Tests CI/CD pipeline thinking.
❌ Common Mistakes:
- Using SQLite in-memory for tests when production is PostgreSQL (different behavior for JSON, ARRAY, datetime)
- Not isolating tests with transactions (test pollution)
- Using a shared test DB across CI jobs (race conditions)
Q136: How do you use ARRAY operators with SQLAlchemy for PostgreSQL?
Technical Answer:
PostgreSQL arrays support containment (@>), overlap (&&), ANY(), and ALL() operators. SQLAlchemy exposes these via column methods and any_() / all_() functions.
Code Example:
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy import String, any_, all_, func
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
tags: Mapped[list] = mapped_column(ARRAY(String), default=list)
with Session(engine) as session:
# Contains element
stmt = select(Article).where(Article.tags.any("python"))
# Contains ALL elements
stmt = select(Article).where(Article.tags.contains(["python", "fastapi"]))
# ANY overlap
stmt = select(Article).where(Article.tags.overlap(["python", "django", "flask"]))
# Compare with ANY of a list (scalar ANY)
stmt = select(User).where(User.role == any_(["admin", "moderator"]))
# Array length
stmt = select(Article).where(func.array_length(Article.tags, 1) > 3)
# Array append (update)
from sqlalchemy import func as F
session.execute(
update(Article)
.where(Article.id == 1)
.values(tags=Article.tags + ["new_tag"])
)
🔍 Interviewer Notes: Array operators avoid expensive JSON lookups for simple list membership checks.
❌ Common Mistakes:
- Using
== any_(...)withfilter_by()(not supported — usewhere()) - Not using a GIN index for
&&and@>operators (full scan) - Using 0-based Python indexing for PostgreSQL array slicing (PostgreSQL is 1-based)
Q137: How do you implement a polymorphic generic foreign key with sqlalchemy_utils?
Technical Answer:
generic_relationship() from sqlalchemy_utils implements a generic FK that can reference any model (like Django's ContentType). Stores object_type (table name) and object_id alongside the data.
Code Example:
from sqlalchemy_utils import generic_relationship
class Comment(Base):
__tablename__ = "comments"
id: Mapped[int] = mapped_column(primary_key=True)
body: Mapped[str] = mapped_column(Text)
# Store what type of object this comment is on
object_type: Mapped[str] = mapped_column(String(100))
object_id: Mapped[int] = mapped_column(Integer)
# Generic relationship — works with any model
object = generic_relationship("object_type", "object_id")
# Usage
with Session(engine) as session:
post = session.get(Post, 1)
article = session.get(Article, 1)
comment1 = Comment(body="Great post!", object=post)
comment2 = Comment(body="Nice article!", object=article)
session.add_all([comment1, comment2])
session.commit()
# Access the related object
c = session.get(Comment, 1)
print(c.object) # <Post id=1>
print(c.object_type) # "posts"
print(c.object_id) # 1
🔍 Interviewer Notes: Generic FKs add flexibility at the cost of referential integrity (no DB-level FK). Tests advanced ORM design knowledge.
❌ Common Mistakes:
- Not validating
object_typeagainst known model types (can store invalid type names) - Trying to use
generic_relationshipin JOINs (not possible — no DB-level FK) - Using this pattern when concrete FKs would work (premature generalization)
Q138: How do you implement two-phase commit with SQLAlchemy?
Technical Answer:
Two-phase commit (2PC) coordinates commits across multiple databases or resources. SQLAlchemy supports TwoPhaseTransaction on engines that support it (PostgreSQL, MySQL). Both phases must succeed for the commit to be durable.
Code Example:
from sqlalchemy import create_engine
primary_engine = create_engine(PRIMARY_URL)
analytics_engine = create_engine(ANALYTICS_URL)
# Two-phase commit across two databases
with primary_engine.connect() as conn1, analytics_engine.connect() as conn2:
# Begin two-phase transactions
t1 = conn1.begin_twophase()
t2 = conn2.begin_twophase()
try:
conn1.execute(text("INSERT INTO orders ..."))
conn2.execute(text("INSERT INTO order_events ..."))
# Phase 1: prepare
t1.prepare()
t2.prepare()
# Phase 2: commit
t1.commit()
t2.commit()
except Exception:
t1.rollback()
t2.rollback()
raise
🔍 Interviewer Notes: 2PC is used for distributed transactions. Tests advanced distributed systems knowledge.
❌ Common Mistakes:
- Using 2PC when eventual consistency (saga pattern) would be simpler and more scalable
- Not handling network failures between prepare and commit (in-doubt transactions)
- Using 2PC with connection pools (pool may not support
PREPARE TRANSACTION)
Q139: How do you write a custom Alembic autogenerate comparison function?
Technical Answer:
Alembic's autogenerate misses some changes by default (partial indexes, column comments, server defaults). Register a custom compare_* function to detect and generate migrations for these.
Code Example:
# env.py
from alembic.autogenerate import comparators, renderers
@comparators.dispatch_for("column")
def compare_column_comment(
autogen_context, modify_ops, schema, tname, cname, conn_col, meta_col
):
"""Detect column comment changes."""
conn_comment = conn_col.get("comment")
meta_comment = meta_col.comment if hasattr(meta_col, "comment") else None
if conn_comment != meta_comment:
from alembic.operations import ops
modify_ops.ops.append(
ops.ModifyTableOps(tname, schema=schema, ops=[
ops.AlterColumnOp(
tname, cname, schema=schema,
existing_comment=conn_comment,
modify_comment=meta_comment,
)
])
)
🔍 Interviewer Notes: Custom autogenerate functions are needed for schema features Alembic doesn't track by default. Tests deep Alembic knowledge.
❌ Common Mistakes:
- Not knowing custom comparators exist (manually writing migrations for column comments every time)
- Registering comparators outside
env.py(won't be picked up during autogenerate) - Writing comparators that generate non-idempotent migrations
Q140: How do you implement connection-level session variables for multi-tenancy?
Technical Answer:
PostgreSQL session variables (set via SET key = value) persist for the connection lifetime. Use pool_events to set tenant-specific variables when connections are checked out from the pool.
Code Example:
from sqlalchemy import event, text
from contextvars import ContextVar
current_tenant: ContextVar[str] = ContextVar("current_tenant", default="public")
@event.listens_for(engine, "connect")
def set_session_defaults(dbapi_conn, conn_record):
cursor = dbapi_conn.cursor()
cursor.execute("SET application_name = 'myapp'")
cursor.close()
@event.listens_for(engine, "checkout")
def set_tenant_context(dbapi_conn, conn_record, conn_proxy):
tenant = current_tenant.get()
cursor = dbapi_conn.cursor()
cursor.execute(f"SET search_path TO {tenant}, public")
cursor.execute(f"SET app.tenant_id = '{tenant}'")
cursor.close()
# FastAPI middleware
@app.middleware("http")
async def tenant_middleware(request: Request, call_next):
tenant = request.headers.get("X-Tenant-ID", "public")
token = current_tenant.set(tenant)
try:
response = await call_next(request)
finally:
current_tenant.reset(token)
return response
🔍 Interviewer Notes: ContextVar + pool events is the correct async-safe approach for per-request connection configuration.
❌ Common Mistakes:
- Using a global variable instead of
ContextVar(tenant bleeds between concurrent requests) - Not resetting
search_pathat connection checkin (next request gets wrong tenant schema) - Not sanitizing the tenant ID before using in
SET search_path(SQL injection)
Q141: How do you use relationship with order_by?
Technical Answer:
Pass order_by= to relationship() to define default ordering for the collection. This ordering is applied whenever the relationship is loaded.
Code Example:
from sqlalchemy.orm import relationship
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
comments: Mapped[List["Comment"]] = relationship(
back_populates="post",
order_by="Comment.created_at.desc()", # string ref to avoid circular import
lazy="selectin",
)
class Comment(Base):
__tablename__ = "comments"
id: Mapped[int] = mapped_column(primary_key=True)
body: Mapped[str] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
post_id: Mapped[int] = mapped_column(ForeignKey("posts.id"))
post: Mapped["Post"] = relationship(back_populates="comments")
with Session(engine) as session:
post = session.scalars(
select(Post).options(selectinload(Post.comments))
).first()
# post.comments is already sorted by created_at DESC
🔍 Interviewer Notes: Default ordering on relationships reduces repetitive ORDER BY in every query, but can be overridden per query when needed.
❌ Common Mistakes:
- Not using string-based
order_bywhen there are circular imports between models - Defining
order_byon aviewonlyrelationship and expecting it to work on the DB query (works but may not apply in all loading strategies) - Mixing Python-side and SQL-side ordering (in-memory sort after loading vs SQL ORDER BY)
Q142: How do you implement a search across multiple columns using or_ and ilike?
Technical Answer:
Combine or_() with .ilike() for case-insensitive pattern matching across multiple columns. For production full-text search, prefer TSVectorType or Elasticsearch, but ilike is sufficient for smaller datasets.
Code Example:
from sqlalchemy import or_, and_, func
def search_users(session: Session, query: str, page: int = 1, per_page: int = 20):
search_term = f"%{query}%"
stmt = (
select(User)
.where(
or_(
User.name.ilike(search_term),
User.email.ilike(search_term),
User.bio.ilike(search_term),
)
)
.order_by(
# Exact match first, then partial
case(
(User.name.ilike(query), 1),
(User.email.ilike(query), 2),
else_=3,
),
User.name,
)
.limit(per_page)
.offset((page - 1) * per_page)
)
return session.scalars(stmt).all()
# PostgreSQL trigram similarity (with pg_trgm extension)
from sqlalchemy import func
stmt = select(User).where(
func.similarity(User.name, query) > 0.3
).order_by(func.similarity(User.name, query).desc())
🔍 Interviewer Notes: ilike is case-insensitive but slow on large tables. Tests awareness of search performance tradeoffs.
❌ Common Mistakes:
- Using
LIKE '%term%'— disables index use (leading wildcard) - Not sanitizing
queryinput for%and_special characters in LIKE patterns - Using
ilikeon millions of rows without a trigram or full-text index
Q143: How do you implement a changelog table that tracks all field changes?
Technical Answer:
Use a before_flush session event to capture attribute history using inspect(obj).attrs. Write change records to a Changelog table within the same transaction.
Code Example:
from sqlalchemy import event, JSON
from sqlalchemy.orm import Session
from sqlalchemy import inspect as sa_inspect
class Changelog(Base):
__tablename__ = "changelog"
id: Mapped[int] = mapped_column(primary_key=True)
entity_type: Mapped[str] = mapped_column(String(100))
entity_id: Mapped[int] = mapped_column(Integer)
changed_by: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
changes: Mapped[dict] = mapped_column(JSON)
changed_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
TRACKED_MODELS = {User, Post, Order}
@event.listens_for(Session, "before_flush")
def log_changes(session, flush_context, instances):
for obj in session.dirty:
if type(obj) not in TRACKED_MODELS:
continue
state = sa_inspect(obj)
changes = {}
for attr in state.attrs:
hist = attr.history
if hist.has_changes() and hist.deleted:
changes[attr.key] = {
"before": hist.deleted[0],
"after": hist.added[0] if hist.added else None,
}
if changes:
session.add(Changelog(
entity_type=obj.__tablename__,
entity_id=obj.id,
changes=changes,
))
🔍 Interviewer Notes: Changelog patterns are common in compliance-heavy domains. Tests event system and attribute history knowledge.
❌ Common Mistakes:
- Tracking changes in
after_commit(history is cleared — usebefore_flush) - Serializing SQLAlchemy type objects directly to JSON (must convert to Python primitives first)
- Not filtering by
TRACKED_MODELS(logs changes toChangelogitself — infinite loop)
Q144: How do you use Literal types in SQLAlchemy for type-safe status columns?
Technical Answer:
Using Literal from typing in Mapped[Literal["a", "b"]] annotations provides IDE and type-checker validation of allowed values at the Python level. Combine with String column and a DB CHECK constraint for full enforcement.
Code Example:
from typing import Literal
from sqlalchemy import CheckConstraint
OrderStatus = Literal["pending", "confirmed", "shipped", "delivered", "cancelled"]
class Order(Base):
__tablename__ = "orders"
__table_args__ = (
CheckConstraint(
"status IN ('pending','confirmed','shipped','delivered','cancelled')",
name="chk_order_status",
),
)
id: Mapped[int] = mapped_column(primary_key=True)
status: Mapped[OrderStatus] = mapped_column(String(20), default="pending")
total: Mapped[float] = mapped_column(Numeric(10, 2))
# mypy/pyright will flag invalid status values
order = Order(status="pending") # OK
order.status = "confirmed" # OK
order.status = "invalid" # type error caught at development time!
with Session(engine) as session:
# Filter with full type safety
confirmed = session.scalars(
select(Order).where(Order.status == "confirmed")
).all()
🔍 Interviewer Notes: Literal types bring compile-time safety to status columns. Tests modern Python typing knowledge applied to SQLAlchemy.
❌ Common Mistakes:
- Not combining
Literalannotations with DBCheckConstraint(type safety only at Python layer, not DB) - Using
EnumwhenLiteral+Stringis simpler and doesn't require DB-level enum type management - Forgetting
Literalis a type hint only — SQLAlchemy column type is stillString
Q145: How do you implement pagination metadata using window functions?
Technical Answer:
Combine func.count().over() window function with the data query to get both total count and rows in a single query — more efficient than two separate queries.
Code Example:
from sqlalchemy import func, select
def paginate_with_count(session: Session, stmt, page: int, per_page: int):
"""Returns data + total in a single query using window function."""
count_col = func.count().over().label("total_count")
stmt_with_count = select(
User, count_col
).select_from(stmt.subquery())
paginated = stmt_with_count.limit(per_page).offset((page - 1) * per_page)
rows = session.execute(paginated).all()
total = rows[0].total_count if rows else 0
items = [row.User for row in rows]
return {
"items": items,
"total": total,
"page": page,
"pages": (total + per_page - 1) // per_page,
}
# Usage
with Session(engine) as session:
base = select(User).where(User.is_active == True).order_by(User.name)
result = paginate_with_count(session, base, page=2, per_page=20)
🔍 Interviewer Notes: Window-function pagination avoids the second COUNT query, reducing round-trips. Tests advanced SQL knowledge.
❌ Common Mistakes:
- Not knowing the window function approach and always running two queries
- Using
func.count()without.over()(aggregate, not window — changes the result shape) - Not handling
rows = []case (would raiseIndexErroronrows[0])
Q146: How do you implement multi-column search with ranking?
Technical Answer:
Combine ts_rank (or trigram similarity) with a UNION of weighted matches to implement relevance-ranked search: exact title match > partial title > body match.
Code Example:
from sqlalchemy import func, literal, union_all, Float
def ranked_search(session: Session, query: str, limit: int = 20):
search_ts = func.to_tsquery("english", query.replace(" ", " & "))
# Rank by relevance using ts_rank
stmt = (
select(
Article.id,
Article.title,
func.ts_rank(Article.search_vector, search_ts).label("rank"),
)
.where(Article.search_vector.op("@@")(search_ts))
.order_by(func.ts_rank(Article.search_vector, search_ts).desc())
.limit(limit)
)
return session.execute(stmt).all()
# Alternative: weighted score using CASE
def search_with_weights(session: Session, q: str, limit: int = 20):
term = f"%{q}%"
score = (
case((Article.title.ilike(q), 10), # exact title
(Article.title.ilike(term), 5), # partial title
else_=1) # body match
).label("score")
stmt = (
select(Article, score)
.where(
or_(Article.title.ilike(term), Article.body.ilike(term))
)
.order_by(score.desc(), Article.title)
.limit(limit)
)
return session.execute(stmt).all()
🔍 Interviewer Notes: Relevance ranking separates basic search from good search UX. Tests SQL + SQLAlchemy expression creativity.
❌ Common Mistakes:
- Returning results in insertion order (no ranking = poor UX)
- Not pre-computing
search_vectoron write (computingto_tsvector()at query time is slow) - Using
ilikewith leading wildcards on large text tables (disables indexes)
Q147: How do you implement automatic updated_at with a DB trigger vs onupdate?
Technical Answer:
onupdate=datetime.utcnow in SQLAlchemy sets updated_at only when updating via the ORM. A DB trigger fires on ALL updates (including bulk SQL, external tools). Both approaches have tradeoffs.
Code Example:
# Approach 1: SQLAlchemy onupdate (ORM-only)
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
updated_at: Mapped[Optional[datetime]] = mapped_column(
DateTime, onupdate=datetime.utcnow, nullable=True
)
# Approach 2: server_default + DB trigger (catches all updates)
class Post(Base):
updated_at: Mapped[datetime] = mapped_column(
DateTime,
server_default=func.now(),
onupdate=func.now(), # for ORM updates
)
# Alembic: DB trigger (catches bulk updates too)
def upgrade():
op.execute("""
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
""")
op.execute("""
CREATE TRIGGER set_updated_at
BEFORE UPDATE ON posts
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
""")
🔍 Interviewer Notes: The trigger approach is more robust for mixed-access environments. Tests understanding of ORM vs DB layer responsibilities.
❌ Common Mistakes:
- Using
onupdate=datetime.utcnow()(called once at class definition — always same timestamp) - Not using a trigger when bulk SQL updates bypass the ORM
- Having both
onupdateand a DB trigger (double update — one overwrites the other)
Q148: How do you use Integer identity columns vs sequences in SQLAlchemy?
Technical Answer:
Integer with primary_key=True uses the DB's default auto-increment (SERIAL in PostgreSQL, AUTOINCREMENT in SQLite). Explicit Sequence objects give you named sequences with custom start/increment values, useful for partitioned tables or distributed ID generation.
Code Example:
from sqlalchemy import Sequence, Integer
# Default auto-increment (maps to SERIAL in PostgreSQL)
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
# Explicit sequence (more control)
user_id_seq = Sequence("user_id_seq", start=1000, increment=1)
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(
Integer,
user_id_seq,
server_default=user_id_seq.next_value(),
primary_key=True,
)
# Get next value without inserting
with engine.connect() as conn:
next_id = conn.scalar(user_id_seq)
# PostgreSQL GENERATED ALWAYS AS IDENTITY (PostgreSQL 10+)
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(
Integer,
primary_key=True,
# Uses GENERATED BY DEFAULT AS IDENTITY
)
🔍 Interviewer Notes: Understanding sequences is important for sharded databases and custom ID schemes.
❌ Common Mistakes:
- Calling
Sequence.next_value()in application code for distributed ID generation (still single point — use UUIDs or Snowflake IDs) - Not resetting sequences after seeding test data with explicit IDs (sequence out of sync)
- Confusing
SERIAL(PostgreSQL-specific shorthand) withIDENTITY(SQL standard, PostgreSQL 10+)
Q149: How do you profile and optimize ORM query performance?
Technical Answer:
Profile with echo=True, query counting, EXPLAIN ANALYZE, and the sqlalchemy_utils query functions. Key optimizations: eager loading for N+1, bulk operations for mass writes, indexed columns for frequent filters.
Code Example:
# 1. Count queries in a test
query_log = []
@event.listens_for(engine, "before_cursor_execute")
def log_query(conn, cursor, statement, params, context, executemany):
query_log.append(statement)
def test_no_n_plus_1():
query_log.clear()
with Session(engine) as session:
posts = session.scalars(
select(Post).options(selectinload(Post.author))
).all()
_ = [p.author.name for p in posts]
assert len(query_log) == 2, f"Expected 2 queries, got {len(query_log)}"
# 2. EXPLAIN ANALYZE
def analyze_query(session, stmt):
explain = session.execute(
text(f"EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) {stmt.compile(engine)}")
).scalar()
return explain
# 3. Use select() with specific columns (avoid SELECT *)
stmt = select(User.id, User.name, User.email) # only load needed columns
# 4. Batch size for large exports
stmt = select(User).execution_options(yield_per=500)
🔍 Interviewer Notes: Query profiling is a practical skill expected of senior engineers. Tests ability to diagnose and fix performance issues.
❌ Common Mistakes:
- Relying on
echo=Truein production (too verbose, logs credentials) - Not running
EXPLAIN ANALYZE(justEXPLAINdoesn't show actual cost) - Optimizing without measuring (premature optimization)
Q150: How do you handle DetachedInstanceError in async FastAPI endpoints?
Technical Answer:
In async FastAPI, DetachedInstanceError occurs when returning ORM objects after the session closes. Fix by: using expire_on_commit=False, converting to Pydantic schemas inside the session, or using lazy="raise" to catch it in development.
Code Example:
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import Depends
# PROBLEM: session closes before serialization
@router.get("/users/{id}")
async def get_user(id: int, session: AsyncSession = Depends(get_db)):
user = await session.get(User, id)
return user # Pydantic serialization may trigger lazy loads AFTER session closes
# FIX 1: Eager load everything needed before session closes
@router.get("/users/{id}")
async def get_user(id: int, session: AsyncSession = Depends(get_db)):
user = await session.scalar(
select(User).options(selectinload(User.posts)).where(User.id == id)
)
return UserSchema.from_orm(user) # convert inside session context
# FIX 2: expire_on_commit=False
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
# FIX 3: Pydantic model_validate inside session
@router.get("/users/{id}")
async def get_user(id: int, session: AsyncSession = Depends(get_db)):
user = await session.get(User, id)
result = UserSchema.model_validate(user) # serialize while session is open
return result
🔍 Interviewer Notes: This is the most common SQLAlchemy + FastAPI production bug. Tests real-world async ORM experience.
❌ Common Mistakes:
- Returning ORM objects directly from FastAPI endpoints (lazy loads triggered by Pydantic serialization after session closes)
- Setting
expire_on_commit=Falseglobally without understanding stale data risk - Not using
selectinloadfor relationships that will be serialized
Q151: How do you implement database-level computed columns?
Technical Answer:
PostgreSQL's GENERATED ALWAYS AS ... STORED creates columns computed at write time. SQLAlchemy maps them with computed= parameter on mapped_column(). These are read-only from the ORM perspective.
Code Example:
from sqlalchemy import Computed
class OrderItem(Base):
__tablename__ = "order_items"
id: Mapped[int] = mapped_column(primary_key=True)
quantity: Mapped[int] = mapped_column(Integer)
unit_price: Mapped[float] = mapped_column(Numeric(10, 2))
# DB computes this — SQLAlchemy won't send it in INSERT/UPDATE
total_price: Mapped[float] = mapped_column(
Numeric(10, 2),
Computed("quantity * unit_price", persisted=True) # STORED
)
# Alembic migration
def upgrade():
op.add_column("order_items", sa.Column(
"total_price",
sa.Numeric(10, 2),
sa.Computed("quantity * unit_price", persisted=True),
))
with Session(engine) as session:
item = OrderItem(quantity=3, unit_price=19.99)
session.add(item)
session.commit()
session.refresh(item)
print(item.total_price) # 59.97 — computed by DB
🔍 Interviewer Notes: DB-computed columns are faster than column_property for frequently-read aggregations because they're pre-computed on write.
❌ Common Mistakes:
- Trying to set a
Computedcolumn in Python (SQLAlchemy raisesArgumentError) - Not using
persisted=True(virtual computed columns recalculate every read — slower) - Using computed columns for expressions involving other tables (not supported)
Q152: How do you use Inspector.get_sorted_table_and_fkc_names() for ordered operations?
Technical Answer:
get_sorted_table_and_fkc_names() returns tables in topological order (parents before children) based on FK relationships. Essential for writing scripts that create or delete tables in the correct dependency order.
Code Example:
from sqlalchemy import inspect
insp = inspect(engine)
sorted_tables = insp.get_sorted_table_and_fkc_names()
print("Tables in dependency order:")
for table_name, fkc in sorted_tables:
print(f" {table_name}")
# Create tables in dependency order (parents first)
metadata = MetaData()
metadata.reflect(bind=engine)
for table_name, _ in insp.get_sorted_table_and_fkc_names():
if table_name in metadata.tables:
metadata.tables[table_name].create(engine, checkfirst=True)
# Delete in reverse order (children first)
for table_name, _ in reversed(insp.get_sorted_table_and_fkc_names()):
if table_name in metadata.tables:
metadata.tables[table_name].drop(engine, checkfirst=True)
🔍 Interviewer Notes: Correct deletion order is critical to avoid FK constraint violations. Tests schema management knowledge.
❌ Common Mistakes:
- Dropping tables in random order (FK constraint violations)
- Using
metadata.drop_all()without knowing the order (SQLAlchemy handles it, but custom scripts don't) - Not filtering by
metadata.tables(includes tables not in your ORM models)
Q153: How do you implement a before_insert event that generates a short ID?
Technical Answer:
Use @event.listens_for(Model, "before_insert") to auto-populate fields before the INSERT fires. Common for slugs, short IDs, or computed display names.
Code Example:
import shortuuid
from sqlalchemy import event
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
slug: Mapped[str] = mapped_column(String(50), unique=True, index=True)
short_id: Mapped[str] = mapped_column(String(12), unique=True, index=True)
@event.listens_for(Article, "before_insert")
def set_short_id(mapper, connection, target):
if not target.short_id:
target.short_id = shortuuid.ShortUUID().random(length=8)
if not target.slug:
import re
target.slug = re.sub(r"[^a-z0-9-]", "-", target.title.lower())[:50]
# More robust: collision detection
@event.listens_for(Article, "before_insert")
def set_unique_slug(mapper, connection, target):
if not target.slug:
base_slug = re.sub(r"[^a-z0-9-]", "-", target.title.lower())[:45]
slug = base_slug
suffix = 1
while connection.execute(
select(func.count()).where(Article.slug == slug)
).scalar() > 0:
slug = f"{base_slug}-{suffix}"
suffix += 1
target.slug = slug
🔍 Interviewer Notes: Slug generation with collision handling is a production reality. Tests event + uniqueness handling.
❌ Common Mistakes:
- Not handling slug collisions (two articles with same title get same slug)
- Using Python
uuid4()as slug (too long — useshortuuidor base62) - Generating slugs in
__init__(doesn't fire for bulk inserts or SQLAlchemy 2.0mapped_columndefaults)
Q154: How do you use op.add_column() with a non-nullable column in Alembic?
Technical Answer:
Adding a NOT NULL column to a table with existing rows requires a default value. Use server_default in the migration to populate existing rows, then optionally remove the default after migration.
Code Example:
def upgrade() -> None:
# Step 1: Add as nullable first (safe for large tables)
op.add_column("users", sa.Column("status", sa.String(20), nullable=True))
# Step 2: Populate existing rows
op.execute("UPDATE users SET status = 'active' WHERE status IS NULL")
# Step 3: Add NOT NULL constraint
op.alter_column("users", "status", nullable=False)
# OR: single step with server_default (adds column + populates in one DDL)
op.add_column("orders", sa.Column(
"currency",
sa.String(3),
nullable=False,
server_default="USD", # populates existing rows
))
# Optionally remove server_default if you want future rows to require explicit value
op.alter_column("orders", "currency", server_default=None)
def downgrade() -> None:
op.drop_column("orders", "currency")
op.drop_column("users", "status")
🔍 Interviewer Notes: The 3-step approach is standard for zero-downtime migrations on large tables.
❌ Common Mistakes:
- Adding
NOT NULLcolumn withoutserver_defaultto non-empty table (constraint violation) - Not removing
server_defaultafter migration (future inserts silently default instead of failing) - Running the 3 steps in separate migrations (risky — intermediate state is nullable)
Q155: How do you implement a polymorphic_on discriminator with a CheckConstraint?
Technical Answer:
Combine polymorphic_on with CheckConstraint to enforce valid discriminator values at the DB level, preventing rows with invalid type values.
Code Example:
from sqlalchemy import CheckConstraint, String
VALID_TYPES = ("admin", "regular", "guest")
class User(Base):
__tablename__ = "users"
__mapper_args__ = {
"polymorphic_on": "user_type",
"polymorphic_identity": "regular",
}
__table_args__ = (
CheckConstraint(
f"user_type IN {VALID_TYPES}",
name="chk_user_type",
),
)
id: Mapped[int] = mapped_column(primary_key=True)
user_type: Mapped[str] = mapped_column(
String(20),
nullable=False,
default="regular",
)
name: Mapped[str] = mapped_column(String(100))
class AdminUser(User):
__mapper_args__ = {"polymorphic_identity": "admin"}
# No extra table — single table inheritance
class GuestUser(User):
__mapper_args__ = {"polymorphic_identity": "guest"}
🔍 Interviewer Notes: DB-level CheckConstraint on discriminator prevents invalid data from external tools or scripts.
❌ Common Mistakes:
- Omitting
CheckConstraint(discriminator column can contain any string — silent data corruption) - Not updating
VALID_TYPESand theCheckConstraintwhen adding a new subclass - Generating the
CheckConstraintwith a Python tuple directly (must be a SQL string)
Q156: How do you implement deferred constraint checking in PostgreSQL?
Technical Answer:
DEFERRABLE INITIALLY DEFERRED constraints are checked at transaction commit rather than per-statement. Useful when inserting data that temporarily violates constraints mid-transaction (e.g., circular FKs or reordering records).
Code Example:
from sqlalchemy import DDL, event
# In Alembic migration
def upgrade():
op.execute("""
ALTER TABLE employees
ADD CONSTRAINT fk_manager FOREIGN KEY (manager_id)
REFERENCES employees(id)
DEFERRABLE INITIALLY DEFERRED
""")
# In SQLAlchemy Core
with engine.begin() as conn:
conn.execute(text("SET CONSTRAINTS fk_manager DEFERRED"))
# Insert circular references safely
conn.execute(text("INSERT INTO employees (id, name, manager_id) VALUES (1, 'CEO', NULL)"))
conn.execute(text("INSERT INTO employees (id, name, manager_id) VALUES (2, 'CTO', 1)"))
# Constraint checked at COMMIT
# For ORM with circular FK
with Session(engine) as session:
session.execute(text("SET CONSTRAINTS ALL DEFERRED"))
emp1 = Employee(id=1, name="Alice")
emp2 = Employee(id=2, name="Bob", manager=emp1)
emp1.manager = emp2 # circular — would fail without deferred constraint
session.add_all([emp1, emp2])
session.commit() # constraint checked here
🔍 Interviewer Notes: Deferred constraints are essential for loading circular relationships and reordering operations.
❌ Common Mistakes:
- Trying to insert circular FKs without deferrable constraints (FK violation on first INSERT)
- Not knowing
SET CONSTRAINTS DEFERREDis connection-scoped (resets after transaction) - Using
DEFERRABLE INITIALLY IMMEDIATEwhenINITIALLY DEFERREDis what's needed
Q157: How do you use column_property with a correlated subquery?
Technical Answer:
column_property() with a correlated subquery computes a value per row using a subquery. Unlike hybrid_property, it's always loaded as part of the SELECT.
Code Example:
from sqlalchemy.orm import column_property
from sqlalchemy import select, func
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# Correlated subquery: post count per user
post_count = column_property(
select(func.count(Post.id))
.where(Post.author_id == id)
.correlate_except(Post)
.scalar_subquery()
)
# Latest post title
latest_post_title = column_property(
select(Post.title)
.where(Post.author_id == id)
.order_by(Post.created_at.desc())
.limit(1)
.correlate_except(Post)
.scalar_subquery()
)
with Session(engine) as session:
users = session.scalars(select(User)).all()
for user in users:
print(f"{user.name}: {user.post_count} posts")
# No extra query — loaded with the main SELECT
🔍 Interviewer Notes: column_property with a subquery adds a correlated SELECT per row — great for small sets, expensive for large tables. Consider a materialized view for production.
❌ Common Mistakes:
- Not using
.correlate_except(Post)(generates uncorrelated subquery — wrong results) - Using
column_propertyfor expressions that reference other tables without a subquery (usehybrid_propertyinstead) - Not knowing this loads on every SELECT (can add significant overhead)
Q158: How do you implement a repository that supports filtering, sorting, and pagination generically?
Technical Answer:
Build a generic QueryBuilder that accepts a list of filter specs, sort specs, and pagination params. This encapsulates SQLAlchemy query construction behind a clean interface.
Code Example:
from dataclasses import dataclass
from typing import Any, Optional
from sqlalchemy import select, asc, desc
@dataclass
class FilterSpec:
field: str
op: str # "eq", "gt", "lt", "like", "in"
value: Any
@dataclass
class SortSpec:
field: str
direction: str = "asc" # "asc" or "desc"
class QueryBuilder:
def __init__(self, model):
self.model = model
self._stmt = select(model)
def filter(self, *specs: FilterSpec):
for spec in specs:
col = getattr(self.model, spec.field)
if spec.op == "eq": self._stmt = self._stmt.where(col == spec.value)
elif spec.op == "gt": self._stmt = self._stmt.where(col > spec.value)
elif spec.op == "lt": self._stmt = self._stmt.where(col < spec.value)
elif spec.op == "like": self._stmt = self._stmt.where(col.ilike(f"%{spec.value}%"))
elif spec.op == "in": self._stmt = self._stmt.where(col.in_(spec.value))
return self
def sort(self, *specs: SortSpec):
for spec in specs:
col = getattr(self.model, spec.field)
self._stmt = self._stmt.order_by(
asc(col) if spec.direction == "asc" else desc(col)
)
return self
def paginate(self, page: int, per_page: int):
self._stmt = self._stmt.limit(per_page).offset((page - 1) * per_page)
return self
def build(self):
return self._stmt
# Usage
stmt = (
QueryBuilder(User)
.filter(FilterSpec("is_active", "eq", True), FilterSpec("age", "gt", 18))
.sort(SortSpec("name", "asc"))
.paginate(page=1, per_page=20)
.build()
)
users = session.scalars(stmt).all()
🔍 Interviewer Notes: Generic query builders appear in admin systems, REST filters, and search APIs. Tests design skills + SQLAlchemy expression API knowledge.
❌ Common Mistakes:
- Not validating
fieldnames (allows arbitrary column access — potential info disclosure) - Not handling type coercion for filter values (string "true" vs boolean
True) - Over-engineering for simple cases (inline
where()is better for 2-3 fixed filters)
Q159: How do you implement connection health monitoring?
Technical Answer:
Monitor pool health using engine events and periodic health-check queries. Expose pool stats (pool.size(), pool.checkedin(), pool.overflow()) for observability.
Code Example:
from sqlalchemy import event, text
from sqlalchemy.pool import Pool
import logging
logger = logging.getLogger("db.pool")
@event.listens_for(Pool, "connect")
def on_connect(dbapi_conn, conn_record):
logger.info("New DB connection established")
@event.listens_for(Pool, "checkout")
def on_checkout(dbapi_conn, conn_record, conn_proxy):
pool = engine.pool
logger.debug(
f"Pool checkout — size: {pool.size()}, "
f"checked in: {pool.checkedin()}, "
f"overflow: {pool.overflow()}"
)
@event.listens_for(Pool, "checkin")
def on_checkin(dbapi_conn, conn_record):
logger.debug("Connection returned to pool")
# Health check endpoint (FastAPI)
@app.get("/health/db")
async def db_health():
try:
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
return {
"status": "healthy",
"pool_size": engine.pool.size(),
"checked_in": engine.pool.checkedin(),
"overflow": engine.pool.overflow(),
}
except Exception as e:
return {"status": "unhealthy", "error": str(e)}
🔍 Interviewer Notes: DB health endpoints and pool monitoring are production-grade requirements. Tests DevOps mindset.
❌ Common Mistakes:
- Not exposing pool metrics (can't diagnose connection exhaustion)
- Health check that only pings once at startup (doesn't detect runtime degradation)
- Using a heavyweight query for health checks (
SELECT 1is sufficient)
Q160: How do you implement a complete CQRS pattern with SQLAlchemy?
Technical Answer:
CQRS separates commands (writes) from queries (reads). Use Session for commands (write, transactional) and a separate read engine/session for queries (optimized SELECT, possibly on replica).
Code Example:
from sqlalchemy.orm import Session
from sqlalchemy.ext.asyncio import AsyncSession
# Command side — writes, uses ORM, transactional
class UserCommandService:
def __init__(self, session: Session):
self.session = session
def create_user(self, name: str, email: str) -> User:
user = User(name=name, email=email)
self.session.add(user)
self.session.flush()
return user
def deactivate_user(self, user_id: int) -> None:
self.session.execute(
update(User).where(User.id == user_id).values(is_active=False)
)
# Query side — reads, optimized SELECTs, possibly denormalized
class UserQueryService:
def __init__(self, session: Session):
self.session = session
def get_user_summary(self, user_id: int) -> dict:
row = self.session.execute(
select(
User.id, User.name, User.email,
func.count(Post.id).label("post_count"),
func.max(Post.created_at).label("last_post"),
)
.outerjoin(Post, Post.author_id == User.id)
.where(User.id == user_id)
.group_by(User.id, User.name, User.email)
).one()
return dict(row._mapping)
# FastAPI wiring
@app.post("/users")
async def create_user(data: UserCreate, session: AsyncSession = Depends(get_db)):
cmd_svc = UserCommandService(session)
user = cmd_svc.create_user(data.name, data.email)
await session.commit()
return {"id": user.id}
🔍 Interviewer Notes: CQRS shows architectural maturity. SQLAlchemy makes it natural to have separate query and command patterns.
❌ Common Mistakes:
- Using the same session for both read-heavy queries and writes (contention)
- Over-applying CQRS to simple CRUD (premature complexity)
- Not handling eventual consistency between write model and read model in full CQRS
Q161: How do you implement a self-referential relationship in SQLAlchemy?
Technical Answer:
A self-referential relationship uses ForeignKey pointing to the same table and a relationship() with remote_side parameter to distinguish parent from child. This models trees (categories, org charts, threaded comments).
Code Example:
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy import ForeignKey
from typing import Optional, List
class Category(Base):
__tablename__ = "categories"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
parent_id: Mapped[Optional[int]] = mapped_column(ForeignKey("categories.id"))
parent: Mapped[Optional["Category"]] = relationship(
"Category", remote_side="Category.id", back_populates="children"
)
children: Mapped[List["Category"]] = relationship(
"Category", back_populates="parent"
)
# Query all root categories with children eagerly loaded
from sqlalchemy import select
from sqlalchemy.orm import selectinload
stmt = (
select(Category)
.where(Category.parent_id.is_(None))
.options(selectinload(Category.children).selectinload(Category.children))
)
roots = session.scalars(stmt).all()
🔍 Interviewer Notes: Tests knowledge of adjacency list pattern and ORM relationship direction.
❌ Common Mistakes:
- Forgetting
remote_side(SQLAlchemy cannot infer join direction) - Using
lazy="joined"on self-referential (produces recursive JOIN explosion) - Not limiting recursion depth in application code
Q162: What is the overlaps parameter in SQLAlchemy relationships?
Technical Answer:
overlaps is a parameter added in SQLAlchemy 1.4 to suppress the SAWarning: relationship X will copy column Y to column Z warning. It tells SQLAlchemy that two relationships intentionally share the same foreign key column, suppressing the overlap detection.
Code Example:
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
user: Mapped["User"] = relationship(back_populates="orders")
# A second relationship that also uses user_id (e.g., via secondary)
user_name: Mapped[str] = column_property(
select(User.name).where(User.id == user_id).correlate_except(User).scalar_subquery()
)
class OrderItem(Base):
__tablename__ = "order_items"
id: Mapped[int] = mapped_column(primary_key=True)
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
order: Mapped["Order"] = relationship(
back_populates="items",
overlaps="user,orders" # suppress overlap warning
)
🔍 Interviewer Notes: Tests familiarity with SQLAlchemy 1.4+ warning system and relationship configuration.
❌ Common Mistakes:
- Blindly suppressing warnings without understanding the underlying FK sharing
- Using
overlapsas a fix for incorrectly modeled relationships - Missing
overlapson both sides of a bidirectional relationship
Q163: How does passive_deletes work in SQLAlchemy?
Technical Answer:
passive_deletes=True tells SQLAlchemy not to load related objects before deletion — instead, it lets the database handle cascade deletes (via ON DELETE CASCADE FK constraint). This is a performance optimization that avoids a SELECT before DELETE.
Code Example:
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
posts: Mapped[List["Post"]] = relationship(
"Post",
back_populates="user",
passive_deletes=True, # DB handles cascade
cascade="all"
)
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(
ForeignKey("users.id", ondelete="CASCADE") # DB-level cascade
)
user: Mapped["User"] = relationship(back_populates="posts")
# DDL must include ON DELETE CASCADE
# When user is deleted, DB cascades — SQLAlchemy does not issue
# SELECT to load posts first
session.delete(user)
session.commit()
🔍 Interviewer Notes: Tests understanding of DB vs ORM cascades and performance implications.
❌ Common Mistakes:
- Using
passive_deleteswithoutON DELETE CASCADEon the FK (orphans remain) - Confusing ORM cascade with DB-level cascade
- Not realizing
passive_deletes="all"also disables SET NULL handling
Q164: How do you use EXPLAIN / EXPLAIN ANALYZE with SQLAlchemy?
Technical Answer:
Use text() with PostgreSQL's EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) to get detailed query plans. For ORM queries, compile the statement to SQL first, then wrap in EXPLAIN.
Code Example:
from sqlalchemy import text, select
from sqlalchemy.dialects import postgresql
# Get compiled SQL from ORM query
stmt = select(User).where(User.email.like("%@example.com")).limit(100)
compiled = stmt.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})
sql_str = str(compiled)
# Run EXPLAIN ANALYZE
with engine.connect() as conn:
result = conn.execute(
text(f"EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) {sql_str}")
)
plan = result.scalar()
import json
plan_data = json.loads(plan)
print(json.dumps(plan_data[0]["Plan"], indent=2))
# Simpler approach: event listener to log all EXPLAIN output
from sqlalchemy import event
@event.listens_for(engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault("query_start_time", []).append(time.time())
🔍 Interviewer Notes: Tests production debugging skills and query optimization approach.
❌ Common Mistakes:
- Using EXPLAIN without ANALYZE (estimated vs actual row counts differ)
- Not looking at Seq Scan on large tables as a red flag
- Forgetting
literal_binds=Truewhich is needed to embed params in the plan
Q165: How do you use partial indexes with SQLAlchemy?
Technical Answer:
Partial indexes (conditional indexes) only index rows matching a WHERE clause. They're smaller, faster to update, and more selective. Define them in __table_args__ using Index(..., postgresql_where=...).
Code Example:
from sqlalchemy import Index, String
from sqlalchemy.orm import Mapped, mapped_column
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
status: Mapped[str] = mapped_column(String(20))
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
__table_args__ = (
# Only index pending orders (tiny fraction of total)
Index(
"ix_orders_pending_user",
"user_id",
"created_at",
postgresql_where=(status == "pending"),
),
# Partial unique index: only one active subscription per user
Index(
"ix_subscriptions_active_unique",
"user_id",
unique=True,
postgresql_where=(status == "active"),
),
)
🔍 Interviewer Notes: Tests knowledge of index optimization for high-cardinality status columns.
❌ Common Mistakes:
- Using a full index when a partial one would cover 99% of queries
- Not matching the WHERE clause exactly in queries (planner won't use the partial index)
- Forgetting that
postgresql_whereis dialect-specific
Q166: How do you handle concurrent Alembic migrations in a multi-instance deployment?
Technical Answer: Use a distributed lock (PostgreSQL advisory lock or Redis lock) to ensure only one instance runs migrations on startup. Alternatively, run migrations as a separate init container in Kubernetes before the app starts.
Code Example:
# alembic/env.py — advisory lock pattern
from sqlalchemy import text
def run_migrations_online():
connectable = engine_from_config(config.get_section(config.config_ini_section))
with connectable.connect() as connection:
# Acquire advisory lock (lock_id is arbitrary but consistent)
MIGRATION_LOCK_ID = 12345
result = connection.execute(
text("SELECT pg_try_advisory_lock(:lock_id)"),
{"lock_id": MIGRATION_LOCK_ID}
)
acquired = result.scalar()
if not acquired:
print("Migration lock held by another process, skipping")
return
try:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
finally:
connection.execute(
text("SELECT pg_advisory_unlock(:lock_id)"),
{"lock_id": MIGRATION_LOCK_ID}
)
🔍 Interviewer Notes: Tests production deployment knowledge — critical for zero-downtime deployments.
❌ Common Mistakes:
- Running migrations from multiple app instances simultaneously (table corruption risk)
- Not releasing advisory lock on failure (but PostgreSQL auto-releases on connection close)
- Using file-based locks that don't work across containers
Q167: How do you manage multiple Alembic heads (branches)?
Technical Answer:
Multiple heads occur when two branches are merged without creating a merge migration. Use alembic merge to create a merge point, or use named branches for feature isolation.
Code Example:
# Check current heads
alembic heads
# Output:
# abc123 (head)
# def456 (head)
# Create a merge migration
alembic merge -m "merge feature_x and feature_y" abc123 def456
# Generates a new migration with:
# down_revision = ("abc123", "def456")
# Named branches for feature isolation
alembic revision --head=base --branch-label=feature_payments \
-m "add payment tables"
# Show branch graph
alembic history --indicate-current
# In generated merge migration
def upgrade():
pass # Nothing to do, just joins the branches
def downgrade():
pass
# Revision with multiple parents:
revision = "ghi789"
down_revision = ("abc123", "def456")
branch_labels = None
depends_on = None
🔍 Interviewer Notes: Tests familiarity with real-world Alembic workflows in team environments.
❌ Common Mistakes:
- Force-setting
headto one branch (loses the other's history) - Not communicating branch merges to the team (others get conflicts)
- Merging without reviewing migration order for dependency conflicts
Q168: How do you use PostgreSQL advisory locks with SQLAlchemy?
Technical Answer:
Advisory locks are application-level locks managed by PostgreSQL. They're useful for distributed job processing, preventing double-processing, and coordinating operations across instances. SQLAlchemy accesses them via text().
Code Example:
from sqlalchemy import text
from contextlib import contextmanager
@contextmanager
def advisory_lock(session, lock_id: int, shared: bool = False, nowait: bool = False):
"""Context manager for PostgreSQL advisory locks."""
if shared:
fn = "pg_advisory_xact_lock_shared" if not nowait else "pg_try_advisory_xact_lock_shared"
else:
fn = "pg_advisory_xact_lock" if not nowait else "pg_try_advisory_xact_lock"
if nowait:
acquired = session.execute(
text(f"SELECT {fn}(:lock_id)"), {"lock_id": lock_id}
).scalar()
if not acquired:
raise LockNotAcquiredError(f"Could not acquire advisory lock {lock_id}")
else:
session.execute(text(f"SELECT {fn}(:lock_id)"), {"lock_id": lock_id})
try:
yield
finally:
pass # xact locks auto-release at transaction end
# Usage: prevent duplicate cron job execution
def process_pending_jobs(session):
JOB_LOCK_ID = hash("pending_jobs") & 0x7FFFFFFF
with advisory_lock(session, JOB_LOCK_ID, nowait=True):
jobs = session.scalars(
select(Job).where(Job.status == "pending").limit(100)
).all()
for job in jobs:
process(job)
session.commit()
🔍 Interviewer Notes: Tests knowledge of distributed coordination without external infrastructure.
❌ Common Mistakes:
- Using session-level locks (
pg_advisory_lock) instead of transaction-level (requires explicit unlock) - Using integer lock IDs that collide between different lock purposes
- Not handling
LockNotAcquiredin nowait scenarios
Q169: How do you use PostgreSQL range types with SQLAlchemy?
Technical Answer:
PostgreSQL range types (int4range, tstzrange, daterange) store ranges natively. Use psycopg2.extras.DateTimeTZRange or psycopg3 range objects with SQLAlchemy's TSTZRANGE/INT4RANGE dialect types.
Code Example:
from sqlalchemy.dialects.postgresql import TSTZRANGE, INT4RANGE
from sqlalchemy import Column
from psycopg2.extras import DateTimeTZRange
from datetime import datetime, timezone
class Reservation(Base):
__tablename__ = "reservations"
id: Mapped[int] = mapped_column(primary_key=True)
resource_id: Mapped[int] = mapped_column(ForeignKey("resources.id"))
# Store reservation as a range
period = Column(TSTZRANGE, nullable=False)
__table_args__ = (
# Exclude overlapping reservations for same resource
# Requires btree_gist extension
# ExcludeConstraint(('resource_id', '='), ('period', '&&'))
)
# Create a reservation
start = datetime(2026, 6, 1, 9, 0, tzinfo=timezone.utc)
end = datetime(2026, 6, 1, 17, 0, tzinfo=timezone.utc)
reservation = Reservation(
resource_id=1,
period=DateTimeTZRange(start, end, bounds="[)")
)
session.add(reservation)
# Query: find reservations overlapping a given range
target = DateTimeTZRange(
datetime(2026, 6, 1, 12, 0, tzinfo=timezone.utc),
datetime(2026, 6, 1, 14, 0, tzinfo=timezone.utc),
)
stmt = select(Reservation).where(
Reservation.period.op("&&")(target) # overlap operator
)
🔍 Interviewer Notes: Tests knowledge of PostgreSQL-specific types for scheduling/booking systems.
❌ Common Mistakes:
- Storing start/end as separate columns (loses range operators, exclusion constraints)
- Forgetting to install
btree_gistextension for exclusion constraints - Not handling timezone-aware datetimes properly
Q170: How do you use PostgreSQL composite types with SQLAlchemy?
Technical Answer:
PostgreSQL composite types are user-defined row types. Map them using SQLAlchemy's TypeDecorator or UserDefinedType, binding/receiving via psycopg2's composite casting. Useful for addresses, money types, coordinates.
Code Example:
from sqlalchemy import UserDefinedType
from sqlalchemy.dialects.postgresql import ARRAY
from psycopg2.extras import register_composite
from psycopg2.extensions import adapt, register_adapter
# PostgreSQL DDL: CREATE TYPE address AS (street TEXT, city TEXT, zip TEXT);
class AddressType(UserDefinedType):
cache_ok = True
def get_col_spec(self):
return "address"
def bind_processor(self, dialect):
def process(value):
if value is None:
return None
return f"({value['street']},{value['city']},{value['zip']})"
return process
def result_processor(self, dialect, coltype):
def process(value):
if value is None:
return None
# psycopg2 returns as a namedtuple if registered
return {"street": value[0], "city": value[1], "zip": value[2]}
return process
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
home_address = Column(AddressType)
# Register with psycopg2 for proper parsing
from sqlalchemy import event
@event.listens_for(engine, "connect")
def on_connect(dbapi_conn, connection_record):
register_composite("address", dbapi_conn, globally=True)
🔍 Interviewer Notes: Tests deep PostgreSQL/SQLAlchemy integration and custom type systems.
❌ Common Mistakes:
- Not registering the composite type on each new connection (use
connectevent) - Forgetting that composite type changes require dropping dependent views/functions
- Using JSON instead (loses type checking and operators)
Q171: How do you use index hints with SQLAlchemy?
Technical Answer:
SQLAlchemy doesn't have built-in index hint support, but PostgreSQL doesn't natively support index hints either. Instead, use partial indexes, enable_seqscan=off for testing, or /*+ IndexScan(table index_name) */ via pg_hint_plan extension with text().
Code Example:
from sqlalchemy import text, select
# Method 1: Use text() with pg_hint_plan comment
stmt = text("""
/*+ IndexScan(orders ix_orders_pending_user) */
SELECT * FROM orders WHERE user_id = :user_id AND status = 'pending'
""")
result = session.execute(stmt, {"user_id": 42}).fetchall()
# Method 2: Force index usage by structuring query differently
# PostgreSQL will use index if query matches partial index predicate exactly
stmt = (
select(Order)
.where(Order.user_id == 42)
.where(Order.status == "pending") # must match partial index WHERE clause
.order_by(Order.created_at.desc())
)
# Method 3: Disable seqscan for testing (not for production!)
with engine.connect() as conn:
conn.execute(text("SET enable_seqscan = off"))
result = conn.execute(select(Order).where(Order.user_id == 42)).fetchall()
conn.execute(text("SET enable_seqscan = on"))
🔍 Interviewer Notes: Tests understanding of PostgreSQL query planner behavior.
❌ Common Mistakes:
- Trying to use MySQL-style
USE INDEXhints (not PostgreSQL) - Assuming
enable_seqscan=offis appropriate in production - Not checking
EXPLAINto verify the planner choice before adding hints
Q172: How do you implement database-level full-text search without extensions?
Technical Answer:
PostgreSQL's native tsvector/tsquery full-text search uses to_tsvector() and to_tsquery(). Create a generated tsvector column and GIN index. SQLAlchemy accesses these via func and cast.
Code Example:
from sqlalchemy.dialects.postgresql import TSVECTOR
from sqlalchemy import Column, Index, func, cast
from sqlalchemy_utils import TSVectorType
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
body: Mapped[str] = mapped_column(Text)
# Generated tsvector column (PostgreSQL 12+)
search_vector = Column(
TSVECTOR,
nullable=True,
)
__table_args__ = (
Index("ix_articles_fts", "search_vector", postgresql_using="gin"),
)
# Populate search vector
def update_search_vector(session, article_id: int):
session.execute(
text("""
UPDATE articles SET search_vector =
setweight(to_tsvector('english', COALESCE(title, '')), 'A') ||
setweight(to_tsvector('english', COALESCE(body, '')), 'B')
WHERE id = :id
"""),
{"id": article_id}
)
# Search query
def search_articles(session, query: str):
tsquery = func.plainto_tsquery("english", query)
stmt = (
select(Article, func.ts_rank(Article.search_vector, tsquery).label("rank"))
.where(Article.search_vector.op("@@")(tsquery))
.order_by(func.ts_rank(Article.search_vector, tsquery).desc())
)
return session.execute(stmt).all()
🔍 Interviewer Notes: Tests PostgreSQL FTS knowledge vs reaching for Elasticsearch prematurely.
❌ Common Mistakes:
- Using
ILIKE '%term%'for search (no index, O(n) scan) - Not weighting title higher than body
- Not handling multi-word queries (use
plainto_tsquerynotto_tsquery)
Q173: How does Alembic handle --autogenerate with custom types?
Technical Answer:
Alembic's autogenerate detects column additions/removals/type changes by comparing MetaData against the database schema. Custom types (TypeDecorator, UserDefinedType) must implement __repr__ correctly for autogenerate to emit the right import.
Code Example:
# Custom type that autogenerates correctly
from sqlalchemy import TypeDecorator, String
import re
class SlugType(TypeDecorator):
impl = String(200)
cache_ok = True
def process_bind_param(self, value, dialect):
if value:
return re.sub(r'[^a-z0-9-]', '-', value.lower())
return value
def process_result_value(self, value, dialect):
return value
# Critical for autogenerate — this is what appears in migration file
def __repr__(self):
return "SlugType()"
# env.py: register type comparator so autogenerate detects changes
from alembic.autogenerate import comparators
# alembic/env.py — add to render_item
def render_item(type_, obj, autogen_context):
if isinstance(obj, SlugType):
autogen_context.imports.add("from myapp.types import SlugType")
return "SlugType()"
return False
# In env.py context.configure:
context.configure(
...,
render_item=render_item,
)
🔍 Interviewer Notes: Tests Alembic customization knowledge for non-trivial type systems.
❌ Common Mistakes:
- Missing
__repr__(autogenerate emitsNullType()) - Not adding imports via
autogen_context.imports - Forgetting
render_itemhook inenv.py
Q174: How do you implement optimistic locking with a hash-based ETag?
Technical Answer:
Beyond integer version_id_col, use a content hash as the ETag. The hash changes when any tracked field changes, enabling HTTP ETag/If-Match validation for REST APIs.
Code Example:
import hashlib
import json
from sqlalchemy import event
class Document(Base):
__tablename__ = "documents"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
content: Mapped[str] = mapped_column(Text)
etag: Mapped[str] = mapped_column(String(64), default="")
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def compute_etag(doc: Document) -> str:
data = {"title": doc.title, "content": doc.content}
return hashlib.sha256(json.dumps(data, sort_keys=True).encode()).hexdigest()[:16]
@event.listens_for(Document, "before_insert")
@event.listens_for(Document, "before_update")
def set_etag(mapper, connection, target):
target.etag = compute_etag(target)
target.updated_at = datetime.utcnow()
# FastAPI endpoint with ETag validation
from fastapi import HTTPException, Header
async def update_document(
doc_id: int,
data: DocumentUpdate,
if_match: Optional[str] = Header(None),
session: AsyncSession = Depends(get_db)
):
doc = await session.get(Document, doc_id)
if if_match and doc.etag != if_match.strip('"'):
raise HTTPException(status_code=412, detail="Precondition Failed")
doc.title = data.title
doc.content = data.content
await session.commit()
return {"etag": doc.etag}
🔍 Interviewer Notes: Tests HTTP caching semantics combined with database concurrency control.
❌ Common Mistakes:
- Not stripping quotes from
If-Matchheader value - Hashing entire object including
updated_at(etag changes even without content change) - Not returning new
ETagin response headers
Q175: How do you implement database connection retry with exponential backoff?
Technical Answer:
Wrap session creation and query execution with retry logic using tenacity. Handle OperationalError (connection lost) differently from IntegrityError (data error — don't retry). Use pool_pre_ping as first line of defense.
Code Example:
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
from sqlalchemy.exc import OperationalError, DisconnectionError
# Engine level: pre_ping handles stale connections automatically
engine = create_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_recycle=3600,
)
# Application level: retry on transient failures
@retry(
retry=retry_if_exception_type((OperationalError, DisconnectionError)),
wait=wait_exponential(multiplier=1, min=1, max=10),
stop=stop_after_attempt(5),
reraise=True,
)
def get_user_with_retry(session_factory, user_id: int):
with session_factory() as session:
return session.get(User, user_id)
# Async version
from tenacity import AsyncRetrying
async def get_user_async(session_factory, user_id: int):
async for attempt in AsyncRetrying(
retry=retry_if_exception_type(OperationalError),
wait=wait_exponential(multiplier=0.5, min=0.5, max=5),
stop=stop_after_attempt(3),
):
with attempt:
async with session_factory() as session:
return await session.get(User, user_id)
🔍 Interviewer Notes: Tests production resilience patterns and understanding of transient vs permanent failures.
❌ Common Mistakes:
- Retrying on
IntegrityError(retry won't fix a unique constraint violation) - Not setting a max retry limit (infinite loop on persistent failure)
- Not logging retry attempts for observability
Q176: How do you use PostgreSQL LISTEN/NOTIFY for real-time events?
Technical Answer:
LISTEN/NOTIFY is PostgreSQL's pub/sub mechanism. SQLAlchemy uses connection.connection.notifies (via psycopg2) to receive notifications asynchronously without polling.
Code Example:
import select
import psycopg2
from sqlalchemy import event, text
# Send notifications from SQLAlchemy events
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
status: Mapped[str] = mapped_column(String(20))
@event.listens_for(Order, "after_update")
def notify_order_update(mapper, connection, target):
if target.status in ("completed", "cancelled"):
connection.execute(
text("SELECT pg_notify(:channel, :payload)"),
{"channel": "order_updates", "payload": f'{{"id": {target.id}, "status": "{target.status}"}}'}
)
# Listener (run in separate thread/process)
def listen_for_orders(dsn: str):
conn = psycopg2.connect(dsn)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute("LISTEN order_updates;")
while True:
if select.select([conn], [], [], 5) != ([], [], []):
conn.poll()
while conn.notifies:
notify = conn.notifies.pop(0)
import json
data = json.loads(notify.payload)
handle_order_update(data)
# Async version with psycopg3
import asyncio
import psycopg
async def listen_async(dsn: str):
async with await psycopg.AsyncConnection.connect(dsn, autocommit=True) as conn:
await conn.execute("LISTEN order_updates")
async for notify in conn.notifies():
print(f"Received: {notify.payload}")
🔍 Interviewer Notes: Tests knowledge of lightweight real-time messaging without Redis/Kafka.
❌ Common Mistakes:
- Using a pooled connection for LISTEN (pool recycles the connection)
- Not setting
autocommit=True(LISTEN requires it) - Relying on NOTIFY for guaranteed delivery (it's fire-and-forget, no persistence)
Q177: How do you implement a test factory pattern for SQLAlchemy models?
Technical Answer:
Use factory_boy with SQLAlchemyModelFactory for creating test fixtures. Factories handle default values, sequences, and related objects, making tests concise and isolated.
Code Example:
import factory
from factory.alchemy import SQLAlchemyModelFactory
from faker import Faker
fake = Faker()
class UserFactory(SQLAlchemyModelFactory):
class Meta:
model = User
sqlalchemy_session = None # Set per-test
sqlalchemy_session_persistence = "commit"
id = factory.Sequence(lambda n: n)
name = factory.LazyFunction(fake.name)
email = factory.LazyAttribute(lambda o: f"user{o.id}@example.com")
is_active = True
created_at = factory.LazyFunction(datetime.utcnow)
class PostFactory(SQLAlchemyModelFactory):
class Meta:
model = Post
sqlalchemy_session = None
title = factory.Faker("sentence", nb_words=5)
body = factory.Faker("paragraph")
author = factory.SubFactory(UserFactory) # Creates related User
# In pytest conftest.py
@pytest.fixture
def db_session(engine):
with Session(engine) as session:
UserFactory._meta.sqlalchemy_session = session
PostFactory._meta.sqlalchemy_session = session
yield session
session.rollback()
# In tests
def test_user_posts(db_session):
user = UserFactory()
posts = PostFactory.create_batch(3, author=user)
assert len(user.posts) == 3
🔍 Interviewer Notes: Tests familiarity with production-quality test infrastructure patterns.
❌ Common Mistakes:
- Using global session (test pollution between tests)
- Not using
create_batchfor bulk creation - Hardcoding IDs instead of using
Sequence
Q178: How do you implement transaction isolation level control per query?
Technical Answer:
Set isolation level per-connection using Connection.execution_options() or engine.connect().execution_options(). PostgreSQL supports READ COMMITTED, REPEATABLE READ, SERIALIZABLE and READ UNCOMMITTED (maps to READ COMMITTED).
Code Example:
from sqlalchemy import text
# Per-connection isolation level
with engine.connect().execution_options(
isolation_level="REPEATABLE READ"
) as conn:
with conn.begin():
# All queries in this transaction see the same snapshot
balance = conn.execute(
text("SELECT balance FROM accounts WHERE id = :id"),
{"id": account_id}
).scalar()
if balance >= amount:
conn.execute(
text("UPDATE accounts SET balance = balance - :amount WHERE id = :id"),
{"amount": amount, "id": account_id}
)
# Serializable for critical financial operations
with engine.connect().execution_options(
isolation_level="SERIALIZABLE"
) as conn:
with conn.begin():
try:
# SQLAlchemy will raise OperationalError on serialization failure
transfer_funds(conn, from_id, to_id, amount)
except Exception as e:
if "could not serialize" in str(e):
# Retry transaction
raise RetryableError(e)
# Session-level
session = Session(engine)
session.connection(execution_options={"isolation_level": "REPEATABLE READ"})
🔍 Interviewer Notes: Tests understanding of ACID isolation levels and when to use each.
❌ Common Mistakes:
- Using SERIALIZABLE everywhere (severe performance impact)
- Not handling serialization failures with retry logic
- Confusing isolation level with locking strategy
Q179: How do you use SQLAlchemy with pytest-postgresql for isolated tests?
Technical Answer:
pytest-postgresql spins up a real PostgreSQL instance per test session. Combine it with SQLAlchemy's transaction rollback pattern for test isolation without recreating the schema for each test.
Code Example:
# conftest.py
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from pytest_postgresql import factories
# Create a postgresql fixture
postgresql_proc = factories.postgresql_proc(port=None)
postgresql = factories.postgresql("postgresql_proc")
@pytest.fixture(scope="session")
def engine(postgresql_proc):
url = f"postgresql:///{postgresql_proc.dbname}?host={postgresql_proc.host}&port={postgresql_proc.port}"
engine = create_engine(url)
Base.metadata.create_all(engine)
yield engine
Base.metadata.drop_all(engine)
@pytest.fixture
def db_session(engine):
"""Each test gets an isolated transaction that's rolled back after."""
connection = engine.connect()
transaction = connection.begin()
session = Session(bind=connection)
# Use savepoints for nested transactions in tests
session.begin_nested()
yield session
session.close()
transaction.rollback()
connection.close()
# Test example
def test_create_user(db_session):
user = User(name="Alice", email="alice@test.com")
db_session.add(user)
db_session.flush()
assert user.id is not None
# Transaction rolled back after test — DB stays clean
🔍 Interviewer Notes: Tests production-quality test infrastructure for database-heavy applications.
❌ Common Mistakes:
- Using
commit()in tests (breaks rollback isolation) - Creating/dropping tables per-test (extremely slow)
- Using SQLite for PostgreSQL-specific features (JSON, arrays, CTEs)
Q180: How do you implement polymorphic queries with with_polymorphic for performance?
Technical Answer:
with_polymorphic("*") issues a single query with outer joins to load all subclass data. Without it, SQLAlchemy issues a separate SELECT per subclass. Use it when you need to filter on subclass attributes in the base query.
Code Example:
from sqlalchemy.orm import with_polymorphic
class Event(Base):
__tablename__ = "events"
__mapper_args__ = {"polymorphic_on": "type", "polymorphic_identity": "event"}
id: Mapped[int] = mapped_column(primary_key=True)
type: Mapped[str] = mapped_column(String(20))
occurred_at: Mapped[datetime] = mapped_column()
class ClickEvent(Event):
__tablename__ = "click_events"
__mapper_args__ = {"polymorphic_identity": "click"}
id: Mapped[int] = mapped_column(ForeignKey("events.id"), primary_key=True)
element: Mapped[str] = mapped_column(String(100))
class PurchaseEvent(Event):
__tablename__ = "purchase_events"
__mapper_args__ = {"polymorphic_identity": "purchase"}
id: Mapped[int] = mapped_column(ForeignKey("events.id"), primary_key=True)
amount: Mapped[float] = mapped_column()
# Single query loading both subclasses
ClickOrPurchase = with_polymorphic(Event, [ClickEvent, PurchaseEvent])
stmt = (
select(ClickOrPurchase)
.where(ClickOrPurchase.occurred_at > cutoff)
# Can filter on subclass attrs
.where(
or_(
ClickOrPurchase.ClickEvent.element == "buy-btn",
ClickOrPurchase.PurchaseEvent.amount > 100
)
)
)
events = session.scalars(stmt).all()
🔍 Interviewer Notes: Tests deep understanding of joined table inheritance and query generation.
❌ Common Mistakes:
- Querying base class without
with_polymorphic(multiple queries, N+1) - Using single-table inheritance for types with very different schemas
- Not indexing the discriminator column
Q181: How do you implement database-level UUID primary keys with SQLAlchemy?
Technical Answer:
Use PostgreSQL's native gen_random_uuid() as server default, or Python's uuid.uuid4() as a Python-side default. Native UUIDs use PostgreSQL's UUID type (16 bytes), not VARCHAR.
Code Example:
import uuid
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import text
class Item(Base):
__tablename__ = "items"
# Option 1: Python-generated UUID
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4
)
# Option 2: Database-generated (PostgreSQL 13+)
# id = Column(UUID(as_uuid=True), primary_key=True,
# server_default=text("gen_random_uuid()"))
name: Mapped[str] = mapped_column(String(200))
# sqlalchemy_utils provides UUIDType for cross-database compat
from sqlalchemy_utils import UUIDType
class Order(Base):
__tablename__ = "orders"
id: Mapped[uuid.UUID] = mapped_column(
UUIDType(binary=False), # Stores as CHAR(32) on DBs without UUID type
primary_key=True,
default=uuid.uuid4
)
# Querying with UUID
item = session.get(Item, uuid.UUID("123e4567-e89b-12d3-a456-426614174000"))
# Foreign key to UUID
class OrderItem(Base):
__tablename__ = "order_items"
id: Mapped[int] = mapped_column(primary_key=True)
order_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("orders.id"))
🔍 Interviewer Notes: Tests knowledge of UUID vs sequential IDs tradeoffs (index fragmentation, security).
❌ Common Mistakes:
- Storing UUIDs as
VARCHAR(36)(wastes space, slower indexing) - Using
uuid1()which leaks MAC address - Not using
as_uuid=True(returns strings instead of UUID objects)
Q182: How do you implement tenant-aware queries with automatic filtering?
Technical Answer:
Use a context variable (contextvars.ContextVar) to store the current tenant ID, then attach a global filter via after_transaction_create event or override Query to always include tenant filtering.
Code Example:
from contextvars import ContextVar
from sqlalchemy import event
from sqlalchemy.orm import Session, QueryContext
import re
current_tenant_id: ContextVar[Optional[int]] = ContextVar("current_tenant_id", default=None)
class TenantMixin:
tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), nullable=False)
class TenantSession(Session):
"""Session that automatically filters by current tenant."""
def _filter_by_tenant(self, stmt):
from sqlalchemy import inspect as sa_inspect
if current_tenant_id.get() is None:
return stmt
# Only filter if entity has tenant_id
for entity in stmt.column_descriptions:
mapper = sa_inspect(entity["entity"], raiseerr=False)
if mapper and hasattr(entity["entity"], "tenant_id"):
stmt = stmt.filter_by(tenant_id=current_tenant_id.get())
return stmt
# Event-based approach (SQLAlchemy 2.0)
@event.listens_for(Session, "do_orm_execute")
def add_tenant_filter(execute_state):
if not execute_state.is_select:
return
tenant_id = current_tenant_id.get()
if tenant_id is None:
return
execute_state.statement = execute_state.statement.filter_by(tenant_id=tenant_id)
# Usage
async def get_orders(session: Session) -> list[Order]:
token = current_tenant_id.set(42)
try:
return session.scalars(select(Order)).all() # Auto-filtered
finally:
current_tenant_id.reset(token)
🔍 Interviewer Notes: Tests multi-tenant architecture patterns and SQLAlchemy extension points.
❌ Common Mistakes:
- Manually adding
.filter(tenant_id=tid)everywhere (easy to forget) - Not resetting context vars in finally block (leaks between requests)
- Not handling super-admin bypass for the tenant filter
Q183: How do you handle large file or blob storage with SQLAlchemy?
Technical Answer:
Don't store large files in the database directly — store paths/URLs and use S3 or filesystem. For small to medium BLOBs, use PostgreSQL's LargeBinary/BYTEA. For streaming, use PostgreSQL Large Objects (OIDs) via psycopg2.extensions.
Code Example:
from sqlalchemy import LargeBinary, String
import boto3
# Pattern 1: Store metadata + S3 key (recommended)
class Attachment(Base):
__tablename__ = "attachments"
id: Mapped[int] = mapped_column(primary_key=True)
filename: Mapped[str] = mapped_column(String(255))
content_type: Mapped[str] = mapped_column(String(100))
size_bytes: Mapped[int] = mapped_column()
s3_key: Mapped[str] = mapped_column(String(500)) # Path in S3
def get_download_url(self, expires_in=3600):
s3 = boto3.client("s3")
return s3.generate_presigned_url(
"get_object",
Params={"Bucket": "my-bucket", "Key": self.s3_key},
ExpiresIn=expires_in
)
# Pattern 2: Store small files as BYTEA (< 1MB thumbnails, etc.)
class Thumbnail(Base):
__tablename__ = "thumbnails"
id: Mapped[int] = mapped_column(primary_key=True)
data: Mapped[bytes] = mapped_column(LargeBinary)
width: Mapped[int] = mapped_column()
height: Mapped[int] = mapped_column()
# Pattern 3: PostgreSQL Large Objects for streaming
def store_large_object(connection, file_path: str) -> int:
raw_conn = connection.connection
lo = raw_conn.lobject(mode="wb")
with open(file_path, "rb") as f:
lo.write(f.read())
lo.close()
return lo.oid # Store this OID in your table
🔍 Interviewer Notes: Tests architectural judgment about database responsibilities.
❌ Common Mistakes:
- Storing large files in BYTEA (bloats WAL, kills backup performance)
- Not deleting S3 objects when DB record is deleted
- Not using presigned URLs (exposing S3 bucket publicly)
Q184: How do you implement a changelog/event sourcing pattern with SQLAlchemy?
Technical Answer:
Use after_update events to write change records to an entity_changes table. Store old/new values as JSON for a lightweight audit log. For full event sourcing, store immutable events and rebuild state via projection.
Code Example:
import json
from sqlalchemy import event, JSON, String, DateTime
from sqlalchemy.orm import attributes
class EntityChange(Base):
__tablename__ = "entity_changes"
id: Mapped[int] = mapped_column(primary_key=True)
entity_type: Mapped[str] = mapped_column(String(100))
entity_id: Mapped[int] = mapped_column()
operation: Mapped[str] = mapped_column(String(10)) # INSERT/UPDATE/DELETE
changed_by: Mapped[Optional[str]] = mapped_column(String(100))
changed_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
old_values: Mapped[Optional[dict]] = mapped_column(JSON)
new_values: Mapped[Optional[dict]] = mapped_column(JSON)
def make_auditable(cls):
"""Decorator to enable changelog for a model."""
@event.listens_for(cls, "after_insert")
def after_insert(mapper, connection, target):
_log_change(connection, "INSERT", target, None, _to_dict(target))
@event.listens_for(cls, "after_update")
def after_update(mapper, connection, target):
history = {}
old_values = {}
for attr in mapper.attrs:
hist = attributes.get_history(target, attr.key)
if hist.has_changes():
old_values[attr.key] = hist.deleted[0] if hist.deleted else None
history[attr.key] = getattr(target, attr.key)
if history:
_log_change(connection, "UPDATE", target, old_values, history)
return cls
def _log_change(connection, op, target, old, new):
connection.execute(
EntityChange.__table__.insert().values(
entity_type=type(target).__name__,
entity_id=target.id,
operation=op,
old_values=old,
new_values=new,
)
)
@make_auditable
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
status: Mapped[str] = mapped_column(String(20))
total: Mapped[float] = mapped_column()
🔍 Interviewer Notes: Tests knowledge of audit patterns and SQLAlchemy event hooks.
❌ Common Mistakes:
- Writing audit records in application code (easy to miss)
- Not capturing
deletedhistory (only recording new values) - Using
after_updatewithout checking if values actually changed
Q185: How do you handle soft deletes with cascading in SQLAlchemy?
Technical Answer:
Soft deletes must propagate to related records — when a User is soft-deleted, their Posts should also be soft-deleted. Implement this via after_soft_delete event or by overriding delete() in a base class.
Code Example:
from datetime import datetime
from sqlalchemy import event
class SoftDeleteMixin:
deleted_at: Mapped[Optional[datetime]] = mapped_column(default=None)
@property
def is_deleted(self) -> bool:
return self.deleted_at is not None
def soft_delete(self, session, cascade_attrs: list[str] | None = None):
self.deleted_at = datetime.utcnow()
if cascade_attrs:
for attr_name in cascade_attrs:
related = getattr(self, attr_name)
if isinstance(related, list):
for item in related:
if hasattr(item, "soft_delete"):
item.soft_delete(session)
elif related and hasattr(related, "soft_delete"):
related.soft_delete(session)
class User(SoftDeleteMixin, Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
posts: Mapped[List["Post"]] = relationship("Post", back_populates="user",
lazy="dynamic")
class Post(SoftDeleteMixin, Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
user: Mapped["User"] = relationship(back_populates="posts")
# Usage
user = session.get(User, 1)
user.soft_delete(session, cascade_attrs=["posts"])
session.commit()
# Global filter (applied in do_orm_execute event)
@event.listens_for(Session, "do_orm_execute")
def filter_soft_deleted(execute_state):
if execute_state.is_select:
for entity_class in _get_entities(execute_state.statement):
if hasattr(entity_class, "deleted_at"):
execute_state.statement = execute_state.statement.filter(
entity_class.deleted_at.is_(None)
)
🔍 Interviewer Notes: Tests consideration for cascading soft deletes — a common production oversight.
❌ Common Mistakes:
- Soft-deleting parent without cascading to children
- Not adding
deleted_at IS NULLto foreign key references in application queries - Forgetting to handle soft-deleted records in unique constraint checks
Q186: How do you implement connection pool monitoring with SQLAlchemy?
Technical Answer:
SQLAlchemy emits pool events (connect, checkout, checkin, invalidate) that you can use to track pool metrics. Expose these as Prometheus metrics for production monitoring.
Code Example:
from sqlalchemy import event, pool
from prometheus_client import Gauge, Counter
DB_POOL_SIZE = Gauge("db_pool_size", "Total connections in pool")
DB_POOL_CHECKED_OUT = Gauge("db_pool_checked_out", "Connections currently in use")
DB_POOL_OVERFLOW = Gauge("db_pool_overflow", "Overflow connections")
DB_POOL_TIMEOUTS = Counter("db_pool_timeouts", "Connection checkout timeouts")
@event.listens_for(engine, "connect")
def on_connect(dbapi_conn, connection_record):
DB_POOL_SIZE.inc()
@event.listens_for(engine, "checkout")
def on_checkout(dbapi_conn, connection_record, connection_proxy):
DB_POOL_CHECKED_OUT.inc()
@event.listens_for(engine, "checkin")
def on_checkin(dbapi_conn, connection_record):
DB_POOL_CHECKED_OUT.dec()
@event.listens_for(engine.pool, "invalidate")
def on_invalidate(dbapi_conn, connection_record, exception):
DB_POOL_SIZE.dec()
# Read pool stats directly
def get_pool_stats() -> dict:
p = engine.pool
return {
"pool_size": p.size(),
"checked_out": p.checkedout(),
"overflow": p.overflow(),
"checked_in": p.checkedin(),
}
# Health check endpoint
from fastapi import APIRouter
router = APIRouter()
@router.get("/health/db")
async def db_health():
stats = get_pool_stats()
if stats["overflow"] > 5:
return {"status": "degraded", "pool": stats}
return {"status": "healthy", "pool": stats}
🔍 Interviewer Notes: Tests production observability mindset for database layer.
❌ Common Mistakes:
- Not monitoring pool exhaustion (silent timeouts under load)
- Setting
pool_sizetoo large (too many connections to PostgreSQL) - Not alerting on
pool_timeoutevents
Q187: How do you implement a database seed script with SQLAlchemy?
Technical Answer:
A seed script populates initial/reference data using INSERT ... ON CONFLICT DO NOTHING (idempotent). Use Alembic data migrations for seed data that's part of the schema, or a separate script for dev/staging data.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
ROLES_SEED = [
{"id": 1, "name": "admin", "permissions": ["read", "write", "delete"]},
{"id": 2, "name": "editor", "permissions": ["read", "write"]},
{"id": 3, "name": "viewer", "permissions": ["read"]},
]
def seed_roles(session: Session):
"""Idempotent role seeding using upsert."""
for role_data in ROLES_SEED:
stmt = pg_insert(Role).values(**role_data)
stmt = stmt.on_conflict_do_update(
index_elements=["id"],
set_={"name": stmt.excluded.name, "permissions": stmt.excluded.permissions}
)
session.execute(stmt)
session.commit()
print(f"Seeded {len(ROLES_SEED)} roles")
def seed_dev_data(session: Session):
"""Non-production sample data."""
if session.scalars(select(func.count(User.id))).one() > 0:
print("Database already has users, skipping dev seed")
return
from factories import UserFactory, PostFactory
UserFactory._meta.sqlalchemy_session = session
admin = UserFactory(email="admin@example.com", role="admin")
PostFactory.create_batch(10, author=admin)
session.commit()
print("Dev seed complete")
if __name__ == "__main__":
engine = create_engine(DATABASE_URL)
with Session(engine) as session:
seed_roles(session)
if os.getenv("ENV") == "development":
seed_dev_data(session)
🔍 Interviewer Notes: Tests data management and idempotency in database operations.
❌ Common Mistakes:
- Using
session.add()without conflict handling (fails on re-run) - Putting dev seed data in production migrations
- Not checking environment before seeding fake data
Q188: How do you implement row-level security (RLS) with SQLAlchemy and PostgreSQL?
Technical Answer:
PostgreSQL RLS policies enforce access rules at the database level using SET app.current_user_id. SQLAlchemy sets this via a before_execute event on each connection. RLS works even if the application bypasses ORM filtering.
Code Example:
from sqlalchemy import event, text
# PostgreSQL DDL for RLS
"""
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders FORCE ROW LEVEL SECURITY;
CREATE POLICY orders_user_isolation ON orders
USING (user_id = current_setting('app.current_user_id')::int);
"""
# SQLAlchemy: set user context before each transaction
@event.listens_for(Session, "after_transaction_create")
def set_user_context(session, transaction):
if transaction.nested:
return
user_id = current_user_id_ctx.get()
if user_id:
session.execute(
text("SET LOCAL app.current_user_id = :uid"),
{"uid": str(user_id)}
)
# Or set at connection checkout
@event.listens_for(engine, "checkout")
def set_rls_on_checkout(dbapi_conn, connection_record, connection_proxy):
user_id = current_user_id_ctx.get()
if user_id:
cursor = dbapi_conn.cursor()
cursor.execute(f"SET app.current_user_id = '{user_id}'")
cursor.close()
# Bypass RLS for admin operations using SET ROLE
def admin_query(session, stmt):
session.execute(text("SET LOCAL ROLE service_account"))
result = session.execute(stmt).all()
session.execute(text("RESET ROLE"))
return result
🔍 Interviewer Notes: Tests knowledge of defense-in-depth — application AND database enforce access control.
❌ Common Mistakes:
- Only enforcing tenant isolation in application code (can be bypassed)
- Not using
SET LOCAL(setting persists across transactions) - Forgetting
FORCE ROW LEVEL SECURITYfor table owner (bypasses policies by default)
Q189: How do you implement select_related equivalent in SQLAlchemy?
Technical Answer:
Django's select_related maps to SQLAlchemy's joinedload(). Use it for many-to-one and one-to-one relationships to avoid N+1 queries. For one-to-many, prefer selectinload() over joinedload() to avoid row multiplication.
Code Example:
from sqlalchemy.orm import joinedload, selectinload, contains_eager
# joinedload: single JOIN query (many-to-one, one-to-one)
stmt = (
select(Post)
.options(
joinedload(Post.author), # JOIN to users
joinedload(Post.author).joinedload(User.profile), # chained
)
.where(Post.published == True)
)
posts = session.scalars(stmt).unique().all()
# selectinload: separate IN query (one-to-many — avoids row multiplication)
stmt = (
select(User)
.options(
selectinload(User.posts), # SELECT ... WHERE user_id IN (...)
selectinload(User.posts).selectinload(Post.comments),
)
.limit(20)
)
users = session.scalars(stmt).all()
# contains_eager: when you've already joined manually
stmt = (
select(Post)
.join(Post.author)
.where(User.country == "US")
.options(contains_eager(Post.author))
)
# Comparison
# joinedload → LEFT OUTER JOIN, duplicates rows for collections
# selectinload → separate IN query, no duplication
# subqueryload → correlated subquery, legacy, usually avoid
🔍 Interviewer Notes: Tests ability to map Django ORM concepts to SQLAlchemy equivalents.
❌ Common Mistakes:
- Using
joinedloadfor collections (row duplication, need.unique()) - Not using any eager loading (N+1 in production)
- Using
subqueryloadoverselectinload(deprecated in 2.0)
Q190: How do you implement polymorphic associations (generic foreign keys) in SQLAlchemy?
Technical Answer:
A generic association allows one table to relate to many different models (e.g., Comment on Post, Photo, or Video). Use a type discriminator column + integer FK, or sqlalchemy_utils.generic_relationship.
Code Example:
from sqlalchemy_utils import generic_relationship
class Comment(Base):
__tablename__ = "comments"
id: Mapped[int] = mapped_column(primary_key=True)
body: Mapped[str] = mapped_column(Text)
# Generic FK: stores any model's table name + PK
commentable_type: Mapped[str] = mapped_column(String(50))
commentable_id: Mapped[int] = mapped_column()
# sqlalchemy_utils handles the magic
commentable = generic_relationship("commentable_type", "commentable_id")
# Usage
post = session.get(Post, 1)
comment = Comment(body="Great post!", commentable=post)
session.add(comment)
session.flush()
# Retrieve
comment = session.get(Comment, 1)
print(comment.commentable) # Returns Post instance
# Manual approach without sqlalchemy_utils:
class Comment(Base):
__tablename__ = "comments"
id: Mapped[int] = mapped_column(primary_key=True)
body: Mapped[str] = mapped_column(Text)
target_type: Mapped[str] = mapped_column(String(50))
target_id: Mapped[int] = mapped_column()
def get_target(self, session):
model_map = {"post": Post, "photo": Photo, "video": Video}
cls = model_map.get(self.target_type)
return session.get(cls, self.target_id) if cls else None
🔍 Interviewer Notes: Tests knowledge of polymorphic association patterns and their trade-offs.
❌ Common Mistakes:
- Using actual FK constraints (can't have a FK pointing to multiple tables)
- Not indexing
(target_type, target_id)composite (slow lookups) - Fetching
get_target()in a loop (N+1 pattern)
Q191: How do you use AsyncSession.run_sync() for synchronous code in async context?
Technical Answer:
run_sync() executes synchronous code in a thread pool within an async SQLAlchemy session. It bridges sync ORM operations (like complex relationship traversal) with async code without blocking the event loop.
Code Example:
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
AsyncSessionLocal = async_sessionmaker(engine, class_=AsyncSession)
async def complex_operation(session: AsyncSession):
# run_sync executes the callable in a thread pool
# 'sync_session' is the underlying synchronous Session
def load_with_sync_orm(sync_session):
# Can use sync ORM features here safely
user = sync_session.get(User, 1)
# Trigger lazy loading (not possible in async context directly)
_ = user.posts # This works in sync context
return user
user = await session.run_sync(load_with_sync_orm)
return user
# More practical: inspecting ORM state synchronously
async def get_schema_info(session: AsyncSession) -> dict:
def _inspect(sync_session):
from sqlalchemy import inspect
inspector = inspect(sync_session.bind)
return inspector.get_table_names()
tables = await session.run_sync(_inspect)
return {"tables": tables}
# Common pattern: running reflection
async def reflect_table(session: AsyncSession, table_name: str):
def _reflect(sync_session):
from sqlalchemy import Table, MetaData
meta = MetaData()
table = Table(table_name, meta, autoload_with=sync_session.bind)
return [(c.name, str(c.type)) for c in table.columns]
return await session.run_sync(_reflect)
🔍 Interviewer Notes: Tests understanding of async/sync boundary management in SQLAlchemy.
❌ Common Mistakes:
- Trying to use lazy loading directly in async context (raises
MissingGreenlet) - Blocking the event loop with sync DB calls instead of using
run_sync - Not understanding that
run_syncstill uses the same connection (no parallelism)
Q192: How do you implement a multi-step wizard with a draft pattern in SQLAlchemy?
Technical Answer:
Store incomplete forms as draft records with a status="draft" discriminator. Use partial unique constraints to allow multiple drafts but only one active record per user. Commit the draft to the active state in a transaction.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
class Application(Base):
__tablename__ = "applications"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
status: Mapped[str] = mapped_column(String(20), default="draft")
step: Mapped[int] = mapped_column(default=1)
data: Mapped[dict] = mapped_column(JSON, default=dict)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
submitted_at: Mapped[Optional[datetime]] = mapped_column(default=None)
__table_args__ = (
# Only one active application per user
Index(
"ix_applications_active_user",
"user_id",
unique=True,
postgresql_where=(status == "active"),
),
)
def save_draft_step(session, user_id: int, step: int, data: dict):
"""Upsert draft data for a specific step."""
draft = session.scalars(
select(Application)
.where(Application.user_id == user_id, Application.status == "draft")
).first()
if draft is None:
draft = Application(user_id=user_id, step=step, data=data)
session.add(draft)
else:
draft.data.update(data)
draft.step = max(draft.step, step)
session.flush()
return draft
def submit_application(session, user_id: int):
"""Atomically convert draft to active."""
draft = session.scalars(
select(Application)
.where(Application.user_id == user_id, Application.status == "draft")
.with_for_update()
).one_or_none()
if not draft:
raise ValueError("No draft application found")
validate_application(draft.data) # Raises if invalid
draft.status = "active"
draft.submitted_at = datetime.utcnow()
session.commit()
return draft
🔍 Interviewer Notes: Tests transaction design and partial uniqueness patterns.
❌ Common Mistakes:
- Using a separate
draftstable (duplicates schema) - Not using
FOR UPDATEwhen transitioning draft→active (race condition) - Not validating the draft before activating
Q193: How do you use text() with bound parameters safely?
Technical Answer:
Always use :param style placeholders in text(), never f-strings or % formatting. Bind parameters via the text() call or execution_options. This prevents SQL injection and allows query plan caching.
Code Example:
from sqlalchemy import text
# CORRECT: bound parameters
stmt = text("SELECT * FROM users WHERE email = :email AND status = :status")
result = session.execute(stmt, {"email": "alice@example.com", "status": "active"})
# CORRECT: typed parameters for complex queries
from sqlalchemy import bindparam, String, Integer
stmt = text("SELECT * FROM users WHERE id = :id")
stmt = stmt.bindparams(bindparam("id", type_=Integer()))
result = session.execute(stmt, {"id": 42})
# WRONG: SQL injection risk
email = "alice@example.com'; DROP TABLE users; --"
stmt = text(f"SELECT * FROM users WHERE email = '{email}'") # NEVER DO THIS
# WRONG: string formatting
stmt = text("SELECT * FROM users WHERE status = '%s'" % status) # NEVER
# Advanced: using textual select with ORM mapping
from sqlalchemy import text
stmt = text("SELECT id, name, email FROM users WHERE department = :dept")
stmt = stmt.columns(User.id, User.name, User.email)
users = session.execute(stmt, {"dept": "engineering"}).mappings().all()
# With RETURNING
stmt = text("""
UPDATE inventory SET quantity = quantity - :qty
WHERE product_id = :pid AND quantity >= :qty
RETURNING id, quantity
""")
result = session.execute(stmt, {"qty": 5, "pid": 100}).fetchone()
🔍 Interviewer Notes: Tests SQL injection awareness — a critical security concept.
❌ Common Mistakes:
- Using f-strings with user input in
text()(SQL injection) - Not using
text()at all and using raw string execution viacursor.execute() - Forgetting that
text()with no params is safe for static SQL
Q194: How do you implement time-series data storage with SQLAlchemy and TimescaleDB?
Technical Answer:
TimescaleDB extends PostgreSQL with hypertables for efficient time-series storage. Use SQLAlchemy with create_hypertable via text(). TimescaleDB creates automatic partitioning by time chunk.
Code Example:
from sqlalchemy import Column, Integer, Float, String, DateTime, text, Index
class Metric(Base):
__tablename__ = "metrics"
# TimescaleDB requires time column in composite PK
time: Mapped[datetime] = mapped_column(primary_key=True)
device_id: Mapped[str] = mapped_column(String(50), primary_key=True)
metric_name: Mapped[str] = mapped_column(String(100))
value: Mapped[float] = mapped_column()
__table_args__ = (
Index("ix_metrics_device_time", "device_id", "time"),
)
# Alembic migration: convert to hypertable
def upgrade():
op.create_table(
"metrics",
sa.Column("time", sa.DateTime(timezone=True), nullable=False),
sa.Column("device_id", sa.String(50), nullable=False),
sa.Column("metric_name", sa.String(100)),
sa.Column("value", sa.Float()),
sa.PrimaryKeyConstraint("time", "device_id"),
)
# Convert to TimescaleDB hypertable
op.execute(
"SELECT create_hypertable('metrics', 'time', chunk_time_interval => INTERVAL '1 day')"
)
# Optional: enable compression
op.execute(
"SELECT add_compression_policy('metrics', INTERVAL '7 days')"
)
# Efficient time-range queries
def query_metrics(session, device_id: str, start: datetime, end: datetime):
stmt = (
select(Metric)
.where(
Metric.device_id == device_id,
Metric.time >= start,
Metric.time < end,
)
.order_by(Metric.time)
)
return session.scalars(stmt).all()
# TimescaleDB continuous aggregates (materialized)
# CREATE MATERIALIZED VIEW hourly_avg AS
# SELECT time_bucket('1 hour', time) AS bucket, device_id, avg(value)
# FROM metrics GROUP BY 1, 2;
🔍 Interviewer Notes: Tests knowledge of time-series databases and PostgreSQL extensions.
❌ Common Mistakes:
- Storing metrics in a regular table without partitioning (query degrades over time)
- Not including time in the primary key (TimescaleDB requirement)
- Not setting
chunk_time_intervalappropriately for query patterns
Q195: How do you implement a full repository pattern with SQLAlchemy?
Technical Answer: The Repository pattern encapsulates data access behind a domain-focused interface. Combine with Unit of Work for transaction management. Repositories should return domain objects, not ORM instances (for strict DDD), or return ORM instances directly (pragmatic approach).
Code Example:
from abc import ABC, abstractmethod
from typing import Optional, List
class UserRepository(ABC):
@abstractmethod
def find_by_id(self, user_id: int) -> Optional[User]: ...
@abstractmethod
def find_by_email(self, email: str) -> Optional[User]: ...
@abstractmethod
def find_active(self, limit: int = 100) -> List[User]: ...
@abstractmethod
def save(self, user: User) -> User: ...
@abstractmethod
def delete(self, user: User) -> None: ...
class SQLAlchemyUserRepository(UserRepository):
def __init__(self, session: Session):
self._session = session
def find_by_id(self, user_id: int) -> Optional[User]:
return self._session.get(User, user_id)
def find_by_email(self, email: str) -> Optional[User]:
return self._session.scalars(
select(User).where(User.email == email)
).first()
def find_active(self, limit: int = 100) -> List[User]:
return self._session.scalars(
select(User).where(User.is_active == True).limit(limit)
).all()
def save(self, user: User) -> User:
self._session.add(user)
self._session.flush()
return user
def delete(self, user: User) -> None:
self._session.delete(user)
class UnitOfWork:
def __init__(self, session_factory):
self._factory = session_factory
def __enter__(self):
self._session = self._factory()
self.users = SQLAlchemyUserRepository(self._session)
return self
def __exit__(self, *args):
self._session.close()
def commit(self):
self._session.commit()
def rollback(self):
self._session.rollback()
# Usage
def register_user(uow: UnitOfWork, name: str, email: str):
with uow:
existing = uow.users.find_by_email(email)
if existing:
raise ValueError("Email already registered")
user = User(name=name, email=email)
uow.users.save(user)
uow.commit()
return user
🔍 Interviewer Notes: Tests architectural knowledge and ability to separate concerns properly.
❌ Common Mistakes:
- Repositories that accept raw SQL or ORM filter expressions (leaks persistence details)
- Not using
flush()aftersave()(ID not available before commit) - Not testing repositories with real database (mock defeats the purpose)
Q196: How do you handle circular imports in SQLAlchemy model definitions?
Technical Answer:
Circular imports occur when Model A imports Model B and B imports A. Solutions: use string-based relationship targets ("User" instead of User), use TYPE_CHECKING guard for type hints, or centralize model registration.
Code Example:
# models/user.py — uses string reference to avoid importing Post
from __future__ import annotations
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
from models.post import Post # Only imported for type checking
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# String reference — SQLAlchemy resolves at mapper configuration time
posts: Mapped[List["Post"]] = relationship("Post", back_populates="author")
# models/post.py
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from models.user import User
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
author: Mapped["User"] = relationship("User", back_populates="posts")
# models/__init__.py — import all models to trigger mapper configuration
from models.user import User
from models.post import Post
# Alternative: centralize all models in one file to avoid circular imports entirely
# base.py
class Base(DeclarativeBase):
pass
# all_models.py
from base import Base
# ... define all models here
🔍 Interviewer Notes: Tests understanding of Python import system and SQLAlchemy's deferred mapper configuration.
❌ Common Mistakes:
- Using direct class imports in relationship() (causes circular import)
- Not using
from __future__ import annotations(string annotations not evaluated) - Not importing all models before calling
Base.metadata.create_all()
Q197: How does SQLAlchemy's identity map prevent duplicate object loading?
Technical Answer:
The identity map is a dictionary keyed by (mapper, primary_key) stored on the Session. When you session.get(User, 1) twice, the second call returns the same Python object — no second SELECT is issued. This ensures in-session consistency.
Code Example:
# Demonstrate identity map
session = Session(engine)
user1 = session.get(User, 1) # Issues SELECT
user2 = session.get(User, 1) # Returns same object from identity map
assert user1 is user2 # Same Python object
print(id(user1) == id(user2)) # True
# Query also uses identity map for known PKs
user3 = session.scalars(select(User).where(User.id == 1)).first()
assert user1 is user3 # Same object!
# expire_on_commit=True means objects expire after commit
session.commit()
# Accessing user1.name now issues a SELECT to refresh
name = user1.name # SELECT issued
# expire_on_commit=False: objects retain values after commit (async use case)
session = Session(engine, expire_on_commit=False)
session.commit()
name = user1.name # No SELECT — cached value used
# Expunge removes from identity map
session.expunge(user1)
user_fresh = session.get(User, 1) # Issues new SELECT
assert user1 is not user_fresh
🔍 Interviewer Notes: Tests understanding of session lifecycle and caching behavior.
❌ Common Mistakes:
- Expecting a fresh SELECT after each
session.get()call - Not knowing that
expire_on_commit=Truecauses lazy loads after commit - Opening multiple sessions to get "fresh" data (use
session.refresh()instead)
Q198: How do you implement a paginated export for millions of rows with SQLAlchemy?
Technical Answer:
Use cursor-based pagination with yield_per() or keyset pagination (WHERE id > last_id). Never use OFFSET for large datasets — it reads and discards rows. Stream via yield_per() to control memory.
Code Example:
from sqlalchemy import select
from typing import Generator, Iterator
def export_all_users(session: Session) -> Iterator[User]:
"""Memory-efficient export using server-side cursor."""
stmt = select(User).order_by(User.id).execution_options(yield_per=1000)
for partition in session.scalars(stmt).partitions(1000):
yield from partition
# Keyset pagination (cursor-based)
def export_with_keyset(session: Session, batch_size: int = 1000) -> Iterator[list[User]]:
last_id = 0
while True:
batch = session.scalars(
select(User)
.where(User.id > last_id)
.order_by(User.id)
.limit(batch_size)
).all()
if not batch:
break
yield batch
last_id = batch[-1].id
# Async streaming export to CSV
import csv
import io
from fastapi import StreamingResponse
async def export_users_csv(session: AsyncSession) -> StreamingResponse:
async def generate():
yield "id,name,email\n"
stmt = select(User).order_by(User.id).execution_options(yield_per=500)
async for partition in (await session.stream_scalars(stmt)).partitions(500):
buf = io.StringIO()
writer = csv.writer(buf)
for user in partition:
writer.writerow([user.id, user.name, user.email])
yield buf.getvalue()
return StreamingResponse(generate(), media_type="text/csv")
🔍 Interviewer Notes: Tests knowledge of memory-efficient data access patterns.
❌ Common Mistakes:
- Using
OFFSETfor pagination (O(n) database work) - Loading all rows into memory before streaming
- Not using
yield_perwithstream_scalarsin async (loads everything)
Q199: How do you handle many-to-many with extra data on the association table?
Technical Answer: Use an explicit association object (model) instead of the SQLAlchemy secondary shortcut. Map the association table as a full model with the extra columns, then create relationships through it.
Code Example:
class Enrollment(Base):
"""Association object with extra data."""
__tablename__ = "enrollments"
student_id: Mapped[int] = mapped_column(ForeignKey("students.id"), primary_key=True)
course_id: Mapped[int] = mapped_column(ForeignKey("courses.id"), primary_key=True)
# Extra association data
enrolled_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
grade: Mapped[Optional[float]] = mapped_column(default=None)
completed: Mapped[bool] = mapped_column(default=False)
student: Mapped["Student"] = relationship(back_populates="enrollments")
course: Mapped["Course"] = relationship(back_populates="enrollments")
class Student(Base):
__tablename__ = "students"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
enrollments: Mapped[List[Enrollment]] = relationship(back_populates="student")
# Convenience: direct access to courses (via association_proxy)
from sqlalchemy.ext.associationproxy import association_proxy
courses = association_proxy("enrollments", "course")
class Course(Base):
__tablename__ = "courses"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
enrollments: Mapped[List[Enrollment]] = relationship(back_populates="course")
# Usage
student = session.get(Student, 1)
course = session.get(Course, 10)
enrollment = Enrollment(student=student, course=course, grade=None)
session.add(enrollment)
session.commit()
# Query with grade filter
enrolled = session.scalars(
select(Student)
.join(Enrollment)
.where(Enrollment.completed == True, Enrollment.grade >= 3.0)
).all()
🔍 Interviewer Notes: Tests understanding of association objects vs secondary tables.
❌ Common Mistakes:
- Using
secondary=table when extra data is needed (can't attach extra columns) - Not defining both sides of the relationship back_populates
- Forgetting the composite PK on the association table
Q200: How do you profile and optimize a slow SQLAlchemy query in production?
Technical Answer:
Use a multi-layer approach: log slow queries, use EXPLAIN ANALYZE, check for N+1, verify indexes. SQLAlchemy's echo=True or event listeners log all SQL. In production, use query logging with execution time thresholds.
Code Example:
import time
import logging
from sqlalchemy import event
log = logging.getLogger("slow_query")
SLOW_QUERY_THRESHOLD_MS = 100
@event.listens_for(engine, "before_cursor_execute")
def before_execute(conn, cursor, stmt, params, context, executemany):
conn.info["query_start"] = time.monotonic()
conn.info["query_stmt"] = stmt
@event.listens_for(engine, "after_cursor_execute")
def after_execute(conn, cursor, stmt, params, context, executemany):
elapsed_ms = (time.monotonic() - conn.info["query_start"]) * 1000
if elapsed_ms > SLOW_QUERY_THRESHOLD_MS:
log.warning(
"SLOW QUERY %.1fms: %s",
elapsed_ms,
conn.info["query_stmt"][:200]
)
# Query optimization checklist:
# 1. Check N+1: count SQL statements for a page of results
# 2. Check missing indexes: EXPLAIN shows Seq Scan on large tables
# 3. Check eager loading: use selectinload/joinedload appropriately
# 4. Check column selection: avoid SELECT * for wide tables
# 5. Check LIMIT: ensure pagination limits are applied early
# Example: optimize a common N+1 pattern
# BAD:
posts = session.scalars(select(Post).limit(20)).all()
for post in posts:
print(post.author.name) # N+1: SELECT per post
# GOOD:
posts = session.scalars(
select(Post)
.options(joinedload(Post.author))
.limit(20)
).unique().all()
for post in posts:
print(post.author.name) # No additional queries
🔍 Interviewer Notes: Tests production debugging skills and systematic optimization approach.
❌ Common Mistakes:
- Optimizing blindly without first profiling
- Adding indexes without checking if the query planner uses them
- Not considering query caching for frequently-run identical queries
Q201: How do you implement database-level sequences with SQLAlchemy?
Technical Answer:
Use Sequence for database-managed sequences that can be shared, pre-allocated, or incremented by custom amounts. Unlike SERIAL/IDENTITY, sequences can be used across multiple tables and support nextval() calls.
Code Example:
from sqlalchemy import Sequence, Column, Integer
# Define sequence explicitly
order_seq = Sequence("order_seq", start=1000, increment=1, metadata=Base.metadata)
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(
Integer,
order_seq,
server_default=order_seq.next_value(),
primary_key=True
)
reference: Mapped[str] = mapped_column(String(20))
# Get next value without inserting
with engine.connect() as conn:
next_id = conn.execute(order_seq)
print(f"Next order ID: {next_id}")
# Shared sequence for multiple tables
invoice_id_seq = Sequence("document_id_seq", metadata=Base.metadata)
class Invoice(Base):
__tablename__ = "invoices"
id: Mapped[int] = mapped_column(Integer, invoice_id_seq,
server_default=invoice_id_seq.next_value(),
primary_key=True)
class Receipt(Base):
__tablename__ = "receipts"
id: Mapped[int] = mapped_column(Integer, invoice_id_seq,
server_default=invoice_id_seq.next_value(),
primary_key=True)
# Alembic: create sequence
def upgrade():
op.execute("CREATE SEQUENCE order_seq START 1000 INCREMENT 1")
op.execute("ALTER TABLE orders ALTER COLUMN id SET DEFAULT nextval('order_seq')")
🔍 Interviewer Notes: Tests knowledge of database sequences vs auto-increment and their use cases.
❌ Common Mistakes:
- Confusing SERIAL (implicit sequence) with explicit Sequence (more control)
- Not setting
metadata=Base.metadata(sequence not included increate_all) - Not handling gaps in sequences (sequences don't guarantee contiguous values)
Q202: How do you implement a "last writer wins" conflict resolution strategy?
Technical Answer:
Last Writer Wins (LWW) uses a timestamp to resolve conflicts — the most recent write wins. Implement with an updated_at timestamp and ON CONFLICT DO UPDATE SET ... WHERE excluded.updated_at > current.updated_at.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
from datetime import datetime, timezone
class SyncedRecord(Base):
__tablename__ = "synced_records"
id: Mapped[str] = mapped_column(String(50), primary_key=True)
device_id: Mapped[str] = mapped_column(String(50))
data: Mapped[dict] = mapped_column(JSON)
updated_at: Mapped[datetime] = mapped_column()
synced_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def sync_record(session, record_id: str, device_id: str, data: dict, client_ts: datetime):
stmt = pg_insert(SyncedRecord).values(
id=record_id,
device_id=device_id,
data=data,
updated_at=client_ts,
synced_at=datetime.now(timezone.utc),
)
stmt = stmt.on_conflict_do_update(
index_elements=["id"],
set_={
"device_id": stmt.excluded.device_id,
"data": stmt.excluded.data,
"updated_at": stmt.excluded.updated_at,
"synced_at": stmt.excluded.synced_at,
},
where=stmt.excluded.updated_at > SyncedRecord.updated_at,
)
session.execute(stmt)
session.commit()
🔍 Interviewer Notes: Tests distributed systems knowledge and conflict resolution patterns.
❌ Common Mistakes:
- Not including
WHERE excluded.updated_at > current.updated_at(older writes overwrite newer) - Trusting client-provided timestamps (can be manipulated or have clock skew)
- Using LWW for financial data (need explicit conflict detection instead)
Q203: How do you implement database-backed rate limiting with SQLAlchemy?
Technical Answer:
Use a rate_limits table with a sliding window counter stored per (user, action, window). PostgreSQL's ON CONFLICT DO UPDATE with atomic increment ensures correctness under concurrent requests.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
from datetime import datetime, timezone, timedelta
class RateLimit(Base):
__tablename__ = "rate_limits"
key: Mapped[str] = mapped_column(String(200), primary_key=True)
window_start: Mapped[datetime] = mapped_column(primary_key=True)
count: Mapped[int] = mapped_column(default=0)
last_request: Mapped[datetime] = mapped_column()
def check_rate_limit(session, user_id: int, action: str,
limit: int = 100, window_minutes: int = 60) -> bool:
now = datetime.now(timezone.utc)
window_start = now.replace(
minute=(now.minute // window_minutes) * window_minutes,
second=0, microsecond=0
)
key = f"{user_id}:{action}"
stmt = pg_insert(RateLimit).values(
key=key, window_start=window_start, count=1, last_request=now
)
stmt = stmt.on_conflict_do_update(
index_elements=["key", "window_start"],
set_={"count": RateLimit.count + 1, "last_request": stmt.excluded.last_request}
).returning(RateLimit.count)
current_count = session.execute(stmt).scalar()
session.commit()
return current_count <= limit
🔍 Interviewer Notes: Tests understanding of atomic DB operations and rate-limiting algorithms.
❌ Common Mistakes:
- Not using atomic upsert (read-then-write race condition)
- Storing rate limits in a database instead of Redis for very high-traffic APIs
- Not cleaning up old windows (unbounded table growth)
Q204: How does SQLAlchemy handle schema migrations for Enum type changes?
Technical Answer:
Adding new values to a PostgreSQL ENUM requires ALTER TYPE ... ADD VALUE which cannot run inside a transaction. Use op.execute() outside a transaction block or set transaction_per_migration=False.
Code Example:
from alembic import op
def upgrade():
# Cannot run inside a transaction block
op.execute("ALTER TYPE order_status ADD VALUE IF NOT EXISTS 'refunded'")
# env.py: allow non-transactional statements
def run_migrations_online():
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
transaction_per_migration=False,
)
with context.begin_transaction():
context.run_migrations()
# Renaming an Enum value (PostgreSQL 10+)
def upgrade():
op.execute("ALTER TYPE order_status RENAME VALUE 'cancelled' TO 'voided'")
# Removing an Enum value: must recreate the type
def upgrade():
op.execute("""
CREATE TYPE order_status_new AS ENUM ('pending', 'active', 'completed');
ALTER TABLE orders ALTER COLUMN status TYPE order_status_new
USING status::text::order_status_new;
DROP TYPE order_status;
ALTER TYPE order_status_new RENAME TO order_status;
""")
🔍 Interviewer Notes: Tests knowledge of PostgreSQL DDL limitations and migration edge cases.
❌ Common Mistakes:
- Running
ALTER TYPE ADD VALUEinside a transaction (PostgreSQL error) - Not using
IF NOT EXISTS(fails on re-run) - Not handling the case where String is used instead of native ENUM
Q205: How do you implement a full-text search index in Alembic with tsvector?
Technical Answer:
Create a tsvector generated column and GIN index via Alembic. PostgreSQL 12+ supports GENERATED ALWAYS AS ... STORED for auto-updating tsvector. Use setweight for field-priority ranking.
Code Example:
from alembic import op
import sqlalchemy as sa
def upgrade():
op.execute("""
ALTER TABLE articles ADD COLUMN search_vector tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', COALESCE(title, '')), 'A') ||
setweight(to_tsvector('english', COALESCE(body, '')), 'B')
) STORED
""")
op.create_index(
"ix_articles_search_vector", "articles", ["search_vector"],
postgresql_using="gin"
)
def downgrade():
op.drop_index("ix_articles_search_vector", table_name="articles")
op.drop_column("articles", "search_vector")
# SQLAlchemy model: map the generated column
from sqlalchemy.dialects.postgresql import TSVECTOR
from sqlalchemy import func
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
body: Mapped[str] = mapped_column(Text)
search_vector = Column(TSVECTOR) # Read-only generated column
def search(session, query: str):
tsq = func.plainto_tsquery("english", query)
return session.scalars(
select(Article)
.where(Article.search_vector.op("@@")(tsq))
.order_by(func.ts_rank(Article.search_vector, tsq).desc())
).all()
🔍 Interviewer Notes: Tests Alembic DDL customization for PostgreSQL-specific features.
❌ Common Mistakes:
- Not using
STORED(virtual generated columns aren't indexable) - Forgetting to populate the column for existing rows
- Not using
plainto_tsquery(handles user input safely)
Q206: How do you test SQLAlchemy models with pytest and a real database?
Technical Answer: Use a session-scoped engine with function-scoped transactions that roll back. Each test runs inside a savepoint (nested transaction) — test changes are invisible to other tests and auto-clean on teardown.
Code Example:
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import Session
@pytest.fixture(scope="session")
def engine():
e = create_engine("postgresql://test:test@localhost/test_db")
Base.metadata.create_all(e)
yield e
Base.metadata.drop_all(e)
@pytest.fixture(scope="session")
def connection(engine):
conn = engine.connect()
yield conn
conn.close()
@pytest.fixture
def db(connection):
transaction = connection.begin()
session = Session(bind=connection)
@event.listens_for(session, "after_transaction_end")
def restart_savepoint(session, transaction):
if transaction.nested and not transaction._parent.nested:
session.begin_nested()
session.begin_nested()
yield session
session.close()
transaction.rollback()
def test_create_user(db):
user = User(name="Test", email="test@example.com")
db.add(user)
db.flush()
assert user.id is not None
🔍 Interviewer Notes: Tests production-quality test isolation patterns for database-heavy applications.
❌ Common Mistakes:
- Using
session.commit()in tests (breaks rollback isolation) - Not using
begin_nested()(SQLAlchemy's SAVEPOINT mechanism) - Using SQLite for PostgreSQL-feature testing
Q207: How do you handle lazy loading MissingGreenlet in async SQLAlchemy?
Technical Answer:
In async SQLAlchemy, accessing a lazy relationship outside an async context raises MissingGreenlet. Fix with eager loading (selectinload/joinedload), explicit await session.refresh(obj, ["attr"]), or use lazy="raise" to detect issues during development.
Code Example:
from sqlalchemy.orm import selectinload
# BAD: triggers MissingGreenlet
async def bad_handler(session: AsyncSession, user_id: int):
user = await session.get(User, user_id)
posts = user.posts # MissingGreenlet!
# GOOD: eager load
async def good_handler(session: AsyncSession, user_id: int):
user = await session.scalar(
select(User).options(selectinload(User.posts)).where(User.id == user_id)
)
posts = user.posts # Already loaded
# GOOD: explicit async refresh
async def refresh_handler(session: AsyncSession, user_id: int):
user = await session.get(User, user_id)
await session.refresh(user, ["posts"])
posts = user.posts
# Detect in development — raise on any lazy access
class User(Base):
__tablename__ = "users"
posts: Mapped[List["Post"]] = relationship("Post", lazy="raise")
🔍 Interviewer Notes: Tests understanding of async SQLAlchemy's concurrency model constraints.
❌ Common Mistakes:
- Using
lazy="dynamic"in async context (deprecated, also breaks) - Forgetting
expire_on_commit=Falsefor async sessions - Using
run_syncto work around lazy loading (defeats async purpose)
Q208: How do you implement optimistic locking with conflict detection and merge?
Technical Answer:
When StaleDataError occurs (version mismatch), fetch the latest version and apply a three-way merge: take client changes that the server didn't modify independently, server wins on conflicts.
Code Example:
from sqlalchemy.exc import StaleDataError
class Document(Base):
__tablename__ = "documents"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(200))
content: Mapped[str] = mapped_column(Text)
version_id: Mapped[int] = mapped_column(default=1)
__mapper_args__ = {"version_id_col": version_id}
def update_with_merge(session, doc_id: int, changes: dict, base_version: int, retries=3):
for attempt in range(retries):
try:
doc = session.get(Document, doc_id)
if doc.version_id != base_version:
# Three-way merge: take client changes if server didn't change same field
for key, value in changes.items():
current = getattr(doc, key)
# Server changed this field independently — skip client change
# Simple policy: server wins on conflict
setattr(doc, key, value)
else:
for key, value in changes.items():
setattr(doc, key, value)
session.commit()
return doc
except StaleDataError:
session.rollback()
if attempt == retries - 1:
raise
🔍 Interviewer Notes: Tests collaborative editing patterns and conflict resolution strategy design.
❌ Common Mistakes:
- Always using "last write wins" without considering user intent
- Not exposing version numbers to the client (needed for conflict detection)
- Not having a retry limit on StaleDataError
Q209: How do you implement a distributed lock with PostgreSQL and SQLAlchemy?
Technical Answer:
PostgreSQL advisory locks provide distributed locking. Use pg_try_advisory_xact_lock() for non-blocking; locks auto-release on transaction end. Convert string keys to consistent 64-bit integers via hash.
Code Example:
from contextlib import contextmanager
from sqlalchemy import text
import hashlib
def _lock_id(key: str) -> int:
return int(hashlib.md5(key.encode()).hexdigest()[:16], 16) & 0x7FFFFFFFFFFFFFFF
@contextmanager
def advisory_lock(session, key: str, nowait: bool = True):
lock_id = _lock_id(key)
if nowait:
acquired = session.execute(
text("SELECT pg_try_advisory_xact_lock(:id)"), {"id": lock_id}
).scalar()
if not acquired:
raise RuntimeError(f"Could not acquire lock: {key}")
else:
session.execute(text("SELECT pg_advisory_xact_lock(:id)"), {"id": lock_id})
yield
# xact lock auto-releases on transaction end
# Usage: prevent duplicate payment processing
def process_payment(session: Session, payment_id: int):
with advisory_lock(session, f"payment:{payment_id}"):
payment = session.get(Payment, payment_id)
if payment.status != "pending":
return
charge_card(payment)
payment.status = "completed"
session.commit()
🔍 Interviewer Notes: Tests distributed coordination patterns without external infrastructure.
❌ Common Mistakes:
- Mixing session-level and transaction-level advisory locks
- Not handling lock acquisition failure gracefully
- Using sequential integer IDs as lock keys (collision between entities)
Q210: How do you implement GraphQL-style field selection optimization with SQLAlchemy?
Technical Answer:
Dynamically construct select() with load_only() based on requested fields. Only eager-load relationships that are explicitly requested. This is equivalent to GraphQL's field projection but for REST APIs.
Code Example:
from sqlalchemy.orm import load_only, selectinload
from typing import Set
def get_users_with_fields(
session: Session, requested_fields: Set[str], include_posts: bool = False
) -> list:
field_map = {
"name": User.name, "email": User.email,
"created_at": User.created_at, "is_active": User.is_active,
}
columns = [User.id] + [col for f, col in field_map.items() if f in requested_fields]
stmt = select(User).options(load_only(*columns))
if include_posts and "posts" in requested_fields:
stmt = stmt.options(selectinload(User.posts).load_only(Post.id, Post.title))
return session.scalars(stmt).all()
# FastAPI endpoint
@router.get("/users")
def list_users(
fields: list[str] = Query(default=["name", "email"]),
session: Session = Depends(get_db)
):
return get_users_with_fields(session, set(fields))
🔍 Interviewer Notes: Tests ability to build dynamic query optimization for API efficiency.
❌ Common Mistakes:
- Always loading all columns (wastes bandwidth for wide tables)
- Not always loading PK (breaks identity map and response serialization)
- Accepting raw field names from users without allowlist validation
Q211: How do you implement connection string encryption for database credentials?
Technical Answer: Never store database credentials in plain text. Use AWS Secrets Manager, HashiCorp Vault, or environment variables. Implement credential rotation by listening for credential change events and recreating the engine.
Code Example:
import boto3
import json
from sqlalchemy import create_engine, event
from threading import Lock
class ManagedEngine:
"""Engine that refreshes credentials from Secrets Manager."""
def __init__(self, secret_name: str, region: str = "us-east-1"):
self._secret_name = secret_name
self._region = region
self._engine = None
self._lock = Lock()
def _get_secret(self) -> dict:
client = boto3.client("secretsmanager", region_name=self._region)
response = client.get_secret_value(SecretId=self._secret_name)
return json.loads(response["SecretString"])
def _create_engine(self, creds: dict):
url = (f"postgresql+psycopg2://{creds['username']}:{creds['password']}"
f"@{creds['host']}:{creds['port']}/{creds['dbname']}")
return create_engine(url, pool_pre_ping=True, pool_size=5)
@property
def engine(self):
with self._lock:
if self._engine is None:
creds = self._get_secret()
self._engine = self._create_engine(creds)
return self._engine
def rotate_credentials(self):
"""Call when secret rotation occurs."""
with self._lock:
old_engine = self._engine
creds = self._get_secret()
self._engine = self._create_engine(creds)
if old_engine:
old_engine.dispose()
# Handle OperationalError from stale credentials
from sqlalchemy.exc import OperationalError
@event.listens_for(engine_manager.engine, "handle_error")
def handle_auth_error(context):
if "password authentication failed" in str(context.original_exception):
engine_manager.rotate_credentials()
🔍 Interviewer Notes: Tests production security and secrets management knowledge.
❌ Common Mistakes:
- Hardcoding credentials in source code or config files
- Not rotating credentials (long-lived static credentials are a security risk)
- Not disposing old engine on rotation (connection leak)
Q212: How do you implement a global query filter for multi-tenant soft delete?
Technical Answer:
Combine tenant filtering and soft-delete filtering in a single do_orm_execute event. Use context vars for the tenant context. The filter is applied automatically to all SELECT statements on models with the relevant columns.
Code Example:
from sqlalchemy import event, inspect as sa_inspect
from sqlalchemy.orm import Session, QueryContext
from contextvars import ContextVar
current_tenant: ContextVar[Optional[int]] = ContextVar("current_tenant", default=None)
bypass_filters: ContextVar[bool] = ContextVar("bypass_filters", default=False)
def _get_model_class(entity):
"""Extract model class from a select entity."""
if hasattr(entity, "mapper"):
return entity.mapper.class_
if isinstance(entity, type):
return entity
return None
@event.listens_for(Session, "do_orm_execute")
def global_filters(execute_state):
if not execute_state.is_select:
return
if bypass_filters.get():
return
tenant_id = current_tenant.get()
for desc in execute_state.statement.column_descriptions:
cls = _get_model_class(desc.get("entity"))
if cls is None:
continue
# Apply tenant filter
if tenant_id and hasattr(cls, "tenant_id"):
execute_state.statement = execute_state.statement.filter(
cls.tenant_id == tenant_id
)
# Apply soft-delete filter
if hasattr(cls, "deleted_at"):
execute_state.statement = execute_state.statement.filter(
cls.deleted_at.is_(None)
)
@contextmanager
def admin_context():
"""Bypass all global filters for admin operations."""
token = bypass_filters.set(True)
try:
yield
finally:
bypass_filters.reset(token)
# Usage
with tenant_context(tenant_id=42):
users = session.scalars(select(User)).all() # Auto-filtered by tenant
with admin_context():
all_users = session.scalars(select(User)).all() # No filters
🔍 Interviewer Notes: Tests SQLAlchemy extension points and multi-tenant architecture patterns.
❌ Common Mistakes:
- Not providing an admin bypass mechanism
- Applying filters to non-ORM queries (raw
text()queries) - Forgetting to reset context vars in finally block
Q213: How do you implement database-level constraints via SQLAlchemy models?
Technical Answer:
Define constraints in __table_args__ using CheckConstraint, UniqueConstraint, ForeignKeyConstraint, and ExcludeConstraint. Named constraints are required for Alembic to detect changes.
Code Example:
from sqlalchemy import CheckConstraint, UniqueConstraint, ForeignKeyConstraint, Index
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(200))
price: Mapped[float] = mapped_column()
discount_price: Mapped[Optional[float]] = mapped_column(default=None)
stock: Mapped[int] = mapped_column(default=0)
category_id: Mapped[int] = mapped_column()
sku: Mapped[str] = mapped_column(String(50))
__table_args__ = (
# Price must be positive
CheckConstraint("price > 0", name="ck_products_price_positive"),
# Discount must be less than price
CheckConstraint(
"discount_price IS NULL OR discount_price < price",
name="ck_products_discount_less_than_price"
),
# Stock cannot be negative
CheckConstraint("stock >= 0", name="ck_products_stock_non_negative"),
# Unique SKU per category
UniqueConstraint("category_id", "sku", name="uq_products_category_sku"),
# FK with explicit name (for Alembic)
ForeignKeyConstraint(
["category_id"], ["categories.id"],
name="fk_products_category_id",
ondelete="RESTRICT"
),
)
# ExcludeConstraint for range types (requires btree_gist)
from sqlalchemy.dialects.postgresql import ExcludeConstraint, TSTZRANGE
class Reservation(Base):
__tablename__ = "reservations"
id: Mapped[int] = mapped_column(primary_key=True)
resource_id: Mapped[int] = mapped_column()
period = Column(TSTZRANGE)
__table_args__ = (
ExcludeConstraint(
("resource_id", "="),
("period", "&&"),
name="excl_reservations_no_overlap"
),
)
🔍 Interviewer Notes: Tests knowledge of database-level data integrity vs application-level validation.
❌ Common Mistakes:
- Not naming constraints (Alembic can't track unnamed constraints)
- Using application validation instead of DB constraints (can be bypassed)
- Forgetting
btree_gistextension forExcludeConstraint
Q214: How do you implement a saga orchestration pattern with SQLAlchemy?
Technical Answer:
A saga manages distributed transactions as a sequence of local transactions with compensating actions. Store saga state in the database to survive failures. Use a SagaExecution table to track step status and enable retry/compensation.
Code Example:
from enum import Enum as PyEnum
class SagaStatus(PyEnum):
RUNNING = "running"
COMPLETED = "completed"
COMPENSATING = "compensating"
FAILED = "failed"
class SagaExecution(Base):
__tablename__ = "saga_executions"
id: Mapped[str] = mapped_column(String(50), primary_key=True)
saga_type: Mapped[str] = mapped_column(String(100))
status: Mapped[str] = mapped_column(String(20), default=SagaStatus.RUNNING.value)
current_step: Mapped[int] = mapped_column(default=0)
context: Mapped[dict] = mapped_column(JSON, default=dict)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
class OrderSaga:
STEPS = [
("reserve_inventory", "release_inventory"),
("charge_payment", "refund_payment"),
("create_shipment", "cancel_shipment"),
]
def __init__(self, session: Session, saga_id: str):
self.session = session
self.saga_id = saga_id
def execute(self, order_data: dict) -> bool:
saga = SagaExecution(id=self.saga_id, saga_type="order", context=order_data)
self.session.add(saga)
self.session.flush()
for i, (action, compensate) in enumerate(self.STEPS):
try:
getattr(self, f"step_{action}")(saga.context)
saga.current_step = i + 1
self.session.flush()
except Exception as e:
saga.status = SagaStatus.COMPENSATING.value
self._compensate(saga, i)
saga.status = SagaStatus.FAILED.value
self.session.commit()
return False
saga.status = SagaStatus.COMPLETED.value
self.session.commit()
return True
def _compensate(self, saga, failed_step: int):
for i in range(failed_step - 1, -1, -1):
_, compensate = self.STEPS[i]
try:
getattr(self, f"step_{compensate}")(saga.context)
except Exception:
pass # Best-effort compensation
🔍 Interviewer Notes: Tests distributed systems patterns and failure handling strategy.
❌ Common Mistakes:
- Not persisting saga state (can't recover from process crash)
- Not implementing idempotent steps (retry causes duplicates)
- Not handling compensation failures (stuck saga)
Q215: How do you implement a cursor-based pagination API with SQLAlchemy?
Technical Answer: Cursor pagination uses an opaque cursor (encoded last-item position) instead of page numbers. This is stable under inserts and works well for infinite scroll. Encode the cursor as a base64-encoded JSON of the last item's sort key.
Code Example:
import base64
import json
from dataclasses import dataclass
from typing import Optional, List
@dataclass
class Page:
items: list
next_cursor: Optional[str]
has_next: bool
def encode_cursor(id: int, created_at: datetime) -> str:
data = {"id": id, "created_at": created_at.isoformat()}
return base64.b64encode(json.dumps(data).encode()).decode()
def decode_cursor(cursor: str) -> dict:
return json.loads(base64.b64decode(cursor.encode()).decode())
def paginate_posts(
session: Session,
cursor: Optional[str] = None,
limit: int = 20,
) -> Page:
stmt = select(Post).order_by(Post.created_at.desc(), Post.id.desc())
if cursor:
data = decode_cursor(cursor)
cursor_ts = datetime.fromisoformat(data["created_at"])
cursor_id = data["id"]
# Items that come "after" the cursor in our sort order
stmt = stmt.where(
or_(
Post.created_at < cursor_ts,
and_(Post.created_at == cursor_ts, Post.id < cursor_id)
)
)
# Fetch one extra to determine if there's a next page
posts = session.scalars(stmt.limit(limit + 1)).all()
has_next = len(posts) > limit
items = posts[:limit]
next_cursor = None
if has_next and items:
last = items[-1]
next_cursor = encode_cursor(last.id, last.created_at)
return Page(items=items, next_cursor=next_cursor, has_next=has_next)
# FastAPI endpoint
@router.get("/posts")
def list_posts(cursor: Optional[str] = None, limit: int = 20,
session: Session = Depends(get_db)):
page = paginate_posts(session, cursor, limit)
return {
"items": page.items,
"next_cursor": page.next_cursor,
"has_next": page.has_next,
}
🔍 Interviewer Notes: Tests pagination strategy knowledge for production APIs.
❌ Common Mistakes:
- Using
OFFSETpagination (unstable under concurrent inserts) - Not using a tie-breaker (if two items have same timestamp, cursor is ambiguous)
- Not base64-encoding cursors (leaks internal schema to clients)
Q216: How do you implement database connection pooling with pgBouncer and SQLAlchemy?
Technical Answer:
pgBouncer is a connection pooler that sits between application and PostgreSQL. With transaction-mode pooling, SQLAlchemy must disable prepared statements and set autocommit differently. Use NullPool or small pool_size since pgBouncer manages connections.
Code Example:
from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
# pgBouncer transaction mode: each transaction uses a different server connection
# Requires: prepared_statements=0 in pgBouncer config
# Option 1: NullPool (pgBouncer manages all pooling)
engine = create_engine(
"postgresql+psycopg2://user:pass@pgbouncer-host:5432/mydb",
poolpre_ping=True,
pool_pre_ping=True,
# Disable prepared statements (incompatible with transaction mode pgBouncer)
connect_args={
"options": "-c statement_timeout=30000",
"keepalives": 1,
},
pool=NullPool, # Don't pool in SQLAlchemy — let pgBouncer do it
)
# Option 2: Small pool for session-mode pgBouncer
engine = create_engine(
"postgresql+psycopg2://user:pass@pgbouncer-host:5432/mydb",
pool_size=2, # Small — pgBouncer multiplexes
max_overflow=0,
pool_pre_ping=True,
pool_recycle=300,
)
# Disable server-side cursors (incompatible with transaction mode)
# Use LIMIT-based pagination instead
# asyncpg with pgBouncer
async_engine = create_async_engine(
"postgresql+asyncpg://user:pass@pgbouncer-host:5432/mydb",
pool_size=5,
# asyncpg uses prepared statement cache — must disable for pgBouncer
connect_args={"statement_cache_size": 0},
)
🔍 Interviewer Notes: Tests production deployment knowledge for high-connection-count scenarios.
❌ Common Mistakes:
- Using prepared statements with pgBouncer transaction mode (causes errors)
- Setting large
pool_sizein SQLAlchemy AND pgBouncer (double pooling, too many connections) - Not setting
statement_cache_size=0with asyncpg
Q217: How do you test database migrations in CI/CD with SQLAlchemy?
Technical Answer: Test migrations in CI by running them against a real PostgreSQL instance. Verify: (1) up migration applies cleanly, (2) down migration undoes it cleanly, (3) schema matches expected state after migration, (4) existing data survives.
Code Example:
# tests/test_migrations.py
import pytest
from alembic.config import Config
from alembic import command
from alembic.script import ScriptDirectory
from sqlalchemy import create_engine, inspect, text
@pytest.fixture(scope="module")
def migrated_db():
"""Fresh database with all migrations applied."""
engine = create_engine("postgresql://test:test@localhost/test_migrations")
alembic_cfg = Config("alembic.ini")
alembic_cfg.set_main_option("sqlalchemy.url", str(engine.url))
# Apply all migrations
command.upgrade(alembic_cfg, "head")
yield engine
# Rollback all migrations
command.downgrade(alembic_cfg, "base")
engine.dispose()
def test_all_migrations_apply_cleanly(migrated_db):
"""All up migrations run without error (fixture ensures this)."""
inspector = inspect(migrated_db)
tables = inspector.get_table_names()
assert "users" in tables
assert "orders" in tables
def test_migration_idempotent(migrated_db):
"""Running migrations twice doesn't cause errors."""
alembic_cfg = Config("alembic.ini")
alembic_cfg.set_main_option("sqlalchemy.url", str(migrated_db.url))
command.upgrade(alembic_cfg, "head") # Should be a no-op
def test_downgrade_and_upgrade(migrated_db):
"""Verify every migration can be reversed."""
alembic_cfg = Config("alembic.ini")
alembic_cfg.set_main_option("sqlalchemy.url", str(migrated_db.url))
script = ScriptDirectory.from_config(alembic_cfg)
revisions = [rev.revision for rev in script.walk_revisions()]
for rev in revisions[:3]: # Test last 3 migrations
command.downgrade(alembic_cfg, "-1")
command.upgrade(alembic_cfg, "+1")
# CI: docker-compose.test.yml starts PostgreSQL, then runs pytest
# GitHub Actions workflow:
# services:
# postgres:
# image: postgres:16
# env: POSTGRES_PASSWORD=test
🔍 Interviewer Notes: Tests understanding of migration testing and CI pipeline design.
❌ Common Mistakes:
- Only testing
upgradebut notdowngrade - Using SQLite in CI for PostgreSQL-specific migrations
- Not testing data migrations preserve existing data
Q218: How do you implement event-driven architecture with SQLAlchemy outbox pattern?
Technical Answer:
The outbox pattern stores domain events in an outbox table within the same database transaction as the business data change. A separate relay process reads the outbox and publishes to Kafka/SQS, ensuring at-least-once delivery.
Code Example:
class OutboxEvent(Base):
__tablename__ = "outbox_events"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
aggregate_type: Mapped[str] = mapped_column(String(100))
aggregate_id: Mapped[str] = mapped_column(String(100))
event_type: Mapped[str] = mapped_column(String(100))
payload: Mapped[dict] = mapped_column(JSON)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
published_at: Mapped[Optional[datetime]] = mapped_column(default=None)
# Write business data + event atomically
def place_order(session: Session, user_id: int, items: list) -> Order:
order = Order(user_id=user_id, status="pending", items=items)
session.add(order)
session.flush() # Get order.id
# Write event to outbox in same transaction
event = OutboxEvent(
aggregate_type="Order",
aggregate_id=str(order.id),
event_type="OrderPlaced",
payload={"order_id": order.id, "user_id": user_id, "item_count": len(items)},
)
session.add(event)
session.commit() # Both order and event committed atomically
return order
# Relay process: reads outbox and publishes
def relay_outbox_events(session: Session, publisher):
unpublished = session.scalars(
select(OutboxEvent)
.where(OutboxEvent.published_at.is_(None))
.order_by(OutboxEvent.created_at)
.limit(100)
.with_for_update(skip_locked=True) # Concurrent-safe
).all()
for event in unpublished:
publisher.publish(event.event_type, event.payload)
event.published_at = datetime.utcnow()
session.commit()
🔍 Interviewer Notes: Tests event-driven architecture patterns and distributed systems reliability.
❌ Common Mistakes:
- Publishing events outside the database transaction (dual-write problem)
- Not using
SKIP LOCKEDin the relay (multiple relays process same events) - Not making event consumers idempotent (at-least-once means duplicates possible)
Q219: How do you implement relationship() with primaryjoin for complex join conditions?
Technical Answer:
Use primaryjoin for non-standard join conditions: joins on non-FK columns, multi-column joins, or conditions involving functions. Combined with foreign_keys parameter to specify which side owns the FK.
Code Example:
from sqlalchemy import and_, or_, func
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
country_code: Mapped[str] = mapped_column(String(2))
# Non-standard: join on multiple conditions
local_orders: Mapped[List["Order"]] = relationship(
"Order",
primaryjoin="and_(User.id == foreign(Order.user_id), "
"User.country_code == foreign(Order.country_code))",
viewonly=True
)
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
country_code: Mapped[str] = mapped_column(String(2))
status: Mapped[str] = mapped_column(String(20))
user: Mapped["User"] = relationship("User", back_populates="orders")
# Join to a non-FK column (email stored in different table)
class EmailAlias(Base):
__tablename__ = "email_aliases"
id: Mapped[int] = mapped_column(primary_key=True)
email: Mapped[str] = mapped_column(String(200), unique=True)
user_id: Mapped[int] = mapped_column()
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
primary_email: Mapped[str] = mapped_column(String(200))
aliases: Mapped[List[EmailAlias]] = relationship(
"EmailAlias",
primaryjoin="User.id == foreign(EmailAlias.user_id)",
foreign_keys="[EmailAlias.user_id]",
viewonly=True,
)
🔍 Interviewer Notes: Tests deep SQLAlchemy relationship configuration for complex schemas.
❌ Common Mistakes:
- Not using
foreign()annotation in string-formprimaryjoin(SQLAlchemy can't infer FK direction) - Forgetting
viewonly=Truefor computed/non-standard joins (write operations fail) - Using
primaryjoinwhen a simple FK relationship withback_populateswould suffice
Q220: How do you implement database health checks with SQLAlchemy?
Technical Answer:
Implement layered health checks: (1) can we get a connection from the pool, (2) can we execute a simple query, (3) is replication lag acceptable. Expose via /health/live (pool available) and /health/ready (query succeeds).
Code Example:
from sqlalchemy import text
from fastapi import APIRouter, Response, status
import time
router = APIRouter()
class DatabaseHealthChecker:
def __init__(self, engine, max_lag_seconds: float = 5.0):
self.engine = engine
self.max_lag = max_lag_seconds
def check_connection(self) -> dict:
"""Basic: can we get and return a connection."""
start = time.monotonic()
try:
with self.engine.connect() as conn:
conn.execute(text("SELECT 1"))
return {"status": "ok", "latency_ms": (time.monotonic() - start) * 1000}
except Exception as e:
return {"status": "error", "error": str(e)}
def check_replication_lag(self) -> dict:
"""Advanced: check replica lag (run on replica)."""
try:
with self.engine.connect() as conn:
lag = conn.execute(
text("SELECT EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))")
).scalar()
if lag is None:
return {"status": "ok", "lag_seconds": 0, "note": "primary"}
if lag > self.max_lag:
return {"status": "degraded", "lag_seconds": lag}
return {"status": "ok", "lag_seconds": lag}
except Exception as e:
return {"status": "error", "error": str(e)}
def get_pool_stats(self) -> dict:
p = self.engine.pool
return {
"size": p.size(),
"checked_out": p.checkedout(),
"overflow": p.overflow(),
}
checker = DatabaseHealthChecker(engine)
@router.get("/health/live")
def liveness():
"""Basic: can we respond at all."""
return {"status": "ok"}
@router.get("/health/ready")
def readiness(response: Response):
"""Full: can we serve database requests."""
result = checker.check_connection()
pool = checker.get_pool_stats()
if result["status"] != "ok" or pool["overflow"] > 10:
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return {"status": "not_ready", "db": result, "pool": pool}
return {"status": "ready", "db": result, "pool": pool}
🔍 Interviewer Notes: Tests production operational knowledge and health check design.
❌ Common Mistakes:
- Using liveness check that hits the database (slow DB kills the process)
- Not distinguishing liveness from readiness
- Not exposing pool stats (valuable for diagnosing connection exhaustion)
3. Advanced Topics
Q221: How does SQLAlchemy 2.0's Mapped type annotation system work under the hood?
Technical Answer:
Mapped[T] is a generic alias that SQLAlchemy's metaclass/mapper reads during class creation. It uses Python's __annotations__ and get_type_hints() to infer column types, nullability (Optional), and relationships (List["Model"]) automatically.
Code Example:
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.orm import class_mapper
from typing import Optional, get_type_hints
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column()
age: Mapped[Optional[int]] = mapped_column(default=None)
# Inspect what SQLAlchemy sees
mapper = class_mapper(User)
for col in mapper.columns:
print(f"{col.name}: type={col.type}, nullable={col.nullable}")
# How annotations are resolved
hints = get_type_hints(User, include_extras=True)
print(hints)
# {'id': Mapped[int], 'name': Mapped[str], 'age': Mapped[Optional[int]]}
# SQLAlchemy introspects Mapped[T] to extract T and determine nullability
# Optional[int] → int + nullable=True
# int → int + nullable=False
# List["Post"] → relationship target
🔍 Interviewer Notes: Understanding annotation resolution explains why Optional controls nullability and List[Model] creates relationships.
❌ Common Mistakes:
- Using
Union[int, None]instead ofOptional[int](both work butOptionalis idiomatic) - Forward-referencing a model with a string
"Post"instead of the actual class (works but loses IDE navigation) - Not understanding that
Mapped[int]is a SQLAlchemy-specific generic — not the same asint
Q222: How do you implement a custom pool class for special connection behavior?
Technical Answer:
Subclass sqlalchemy.pool.Pool to implement custom connection lifecycle logic — e.g., a pool that injects session variables on every checkout, a pool with circuit-breaker behavior, or a pool backed by a connection broker.
Code Example:
from sqlalchemy.pool import QueuePool
class TenantAwarePool(QueuePool):
"""Pool that sets search_path on every connection checkout."""
def _do_get(self):
conn = super()._do_get()
return conn
def connect(self):
conn = super().connect()
return conn
# More practical: use pool events instead of subclassing
from sqlalchemy import event
from sqlalchemy.pool import Pool
@event.listens_for(Pool, "checkout")
def on_checkout(dbapi_conn, conn_record, conn_proxy):
"""Run on every connection checkout from pool."""
cursor = dbapi_conn.cursor()
# Set application-level session variable
cursor.execute("SET lock_timeout = '5s'")
cursor.execute("SET statement_timeout = '30s'")
cursor.close()
engine = create_engine(
DATABASE_URL,
poolclass=QueuePool,
pool_size=10,
max_overflow=5,
)
🔍 Interviewer Notes: Pool events are the practical tool; subclassing is rarely needed. Tests understanding of extension points.
❌ Common Mistakes:
- Subclassing
Poolwhen@event.listens_for(Pool, "checkout")would suffice - Setting heavy per-checkout configurations (latency on every connection use)
- Not handling exceptions in pool events (swallows connection errors)
Q223: How do you implement query caching with SQLAlchemy?
Technical Answer:
SQLAlchemy doesn't have a built-in query cache, but you can implement one using dogpile.cache or Redis. Cache the serialized results, not the ORM objects (detached objects can't be re-used across sessions).
Code Example:
import json
import redis
from functools import wraps
cache = redis.Redis(host="localhost", port=6379)
def cache_query(key_prefix: str, ttl: int = 300):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
cache_key = f"{key_prefix}:{':'.join(str(a) for a in args[1:])}"
cached = cache.get(cache_key)
if cached:
return json.loads(cached)
result = func(*args, **kwargs)
# Serialize to dict before caching (ORM objects can't be pickled safely)
serialized = [
{"id": u.id, "name": u.name, "email": u.email}
for u in result
]
cache.setex(cache_key, ttl, json.dumps(serialized))
return result
return wrapper
return decorator
class UserService:
def __init__(self, session: Session):
self.session = session
@cache_query("users:active", ttl=60)
def get_active_users(self):
return self.session.scalars(
select(User).where(User.is_active == True).order_by(User.name)
).all()
def invalidate_user_cache(self):
for key in cache.scan_iter("users:active:*"):
cache.delete(key)
🔍 Interviewer Notes: Caching ORM objects requires serialization — you can't store the SQLAlchemy object in Redis. Tests awareness of session/object lifecycle.
❌ Common Mistakes:
- Trying to pickle ORM objects with active lazy-load proxies (fails or loads entire DB)
- Not invalidating cache on writes (stale data)
- Caching at the ORM layer instead of at the HTTP layer (misses HTTP-level caching mechanisms)
Q224: How do you implement sharding with SQLAlchemy?
Technical Answer:
SQLAlchemy's ShardedSession (from sqlalchemy.ext.horizontal_shard) distributes queries across multiple database shards. Define shard chooser and ID generator functions to route reads/writes.
Code Example:
from sqlalchemy.ext.horizontal_shard import ShardedSession, set_shard_id
from sqlalchemy import create_engine
shard_engines = {
"shard1": create_engine("postgresql://host1/db"),
"shard2": create_engine("postgresql://host2/db"),
"shard3": create_engine("postgresql://host3/db"),
}
def shard_chooser(mapper, instance, clause=None):
"""Route writes to the correct shard based on user_id."""
if hasattr(instance, "user_id"):
return f"shard{(instance.user_id % 3) + 1}"
return "shard1"
def id_chooser(query, ident):
"""Route reads by primary key to the correct shard."""
return [f"shard{(ident[0] % 3) + 1}"]
def query_chooser(query):
"""Fan out to all shards for non-PK queries."""
return list(shard_engines.keys())
ShardedSessionFactory = sessionmaker(
class_=ShardedSession,
shards=shard_engines,
shard_chooser=shard_chooser,
id_chooser=id_chooser,
query_chooser=query_chooser,
)
🔍 Interviewer Notes: SQLAlchemy's built-in sharding is suitable for simple cases. Production sharding usually needs a proxy (PgBouncer + Citus or Vitess).
❌ Common Mistakes:
- Cross-shard JOINs (not supported — must be done in application layer)
- Using
query_chooserthat fans out to all shards for every query (performance anti-pattern) - Not using a consistent hash for shard routing (data moves when shards are added)
Q225: How does SQLAlchemy compile and cache SQL expressions?
Technical Answer: SQLAlchemy uses a compiled cache keyed by the structure of the SQL expression (not values — those are bound parameters). The cache stores the compiled string to avoid re-parsing on subsequent identical queries.
Code Example:
from sqlalchemy import select
from sqlalchemy.dialects import postgresql
# See the compiled SQL
stmt = select(User).where(User.age > 18)
compiled = stmt.compile(dialect=postgresql.dialect())
print(str(compiled)) # SELECT users.id, users.name ... WHERE users.age > %(age_1)s
print(compiled.params) # {'age_1': 18}
# SQLAlchemy caches the compiled form per engine
# Verify caching is working
engine = create_engine(DATABASE_URL, echo=True)
with Session(engine) as session:
session.scalars(select(User).where(User.age > 18)).all() # compiles + caches
session.scalars(select(User).where(User.age > 25)).all() # uses cache (same structure)
# Disable caching for a specific statement (dynamic SQL)
stmt = select(User).execution_options(compiled_cache=None)
# TypeDecorator requires cache_ok = True
class MyType(TypeDecorator):
impl = String
cache_ok = True # required for caching compatibility
🔍 Interviewer Notes: The compiled cache is why SQLAlchemy ORM queries are much faster on repeated calls. Tests deep framework knowledge.
❌ Common Mistakes:
- Using
str(stmt)for SQL logging (misses bound params — usecompiled.string + str(compiled.params)) - Not setting
cache_ok = Truein customTypeDecorator(emitsSAWarning, caching disabled) - Generating structural differences per call (lambda-generated
WHEREwith different column per call — no caching)
Q226: How do you implement async bulk operations efficiently?
Technical Answer:
For async bulk inserts, use session.execute(insert(Model), list_of_dicts) with async with session.begin(). For very large volumes, use copy_records_to_table via asyncpg directly.
Code Example:
import asyncpg
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
async_engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
# Async bulk insert with SQLAlchemy
async def bulk_insert_users(session: AsyncSession, users: list[dict]):
await session.execute(insert(User), users)
await session.commit()
# High-performance: asyncpg COPY (fastest possible)
async def ultra_bulk_insert(records: list[dict]):
conn = await asyncpg.connect("postgresql://user:pass@localhost/db")
await conn.copy_records_to_table(
"users",
records=[(r["name"], r["email"]) for r in records],
columns=["name", "email"],
)
await conn.close()
# Chunked bulk insert for memory management
async def chunked_bulk_insert(session: AsyncSession, records: list[dict], chunk_size=1000):
for i in range(0, len(records), chunk_size):
chunk = records[i:i + chunk_size]
await session.execute(insert(User), chunk)
await session.flush()
await session.commit()
🔍 Interviewer Notes: asyncpg.copy_records_to_table is 5-10x faster than INSERT for bulk loads. Tests performance-critical async knowledge.
❌ Common Mistakes:
- Using
session.add()in a loop in async code (no benefit from async — N individual INSERTs) - Not chunking large inserts (memory pressure or lock escalation)
- Mixing asyncpg direct calls with SQLAlchemy in the same transaction (different connections)
Q227: How do you implement retry logic for deadlocks?
Technical Answer:
Deadlocks are transient — the correct response is to retry the transaction. Detect OperationalError with deadlock error codes (40001 in PostgreSQL) and retry with exponential backoff.
Code Example:
import asyncio
from sqlalchemy.exc import OperationalError
from psycopg2.errors import DeadlockDetected
DEADLOCK_PGCODE = "40001"
async def with_deadlock_retry(session_factory, operation, max_retries=3):
for attempt in range(max_retries):
async with session_factory() as session:
try:
result = await operation(session)
await session.commit()
return result
except OperationalError as e:
await session.rollback()
if hasattr(e.orig, "pgcode") and e.orig.pgcode == DEADLOCK_PGCODE:
if attempt < max_retries - 1:
wait = 0.1 * (2 ** attempt)
await asyncio.sleep(wait)
continue
raise
# Usage
async def transfer_funds(session, from_id, to_id, amount):
from_acc = await session.get(Account, from_id, with_for_update=True)
to_acc = await session.get(Account, to_id, with_for_update=True)
from_acc.balance -= amount
to_acc.balance += amount
await with_deadlock_retry(
AsyncSessionLocal,
lambda s: transfer_funds(s, 1, 2, 100.0)
)
🔍 Interviewer Notes: Deadlock retry is production-critical for concurrent financial or inventory operations. Tests distributed systems error handling.
❌ Common Mistakes:
- Not retrying deadlocks (application returns 500 instead of succeeding on retry)
- Retrying without rollback (session in broken state)
- Using fixed sleep instead of exponential backoff (thundering herd on high concurrency)
Q228: How do you implement a saga pattern with SQLAlchemy for distributed transactions?
Technical Answer:
The saga pattern replaces 2PC with a sequence of local transactions, each with a compensating transaction for rollback. Use a SagaLog table to track progress and enable recovery.
Code Example:
from enum import Enum
class SagaStatus(str, Enum):
STARTED = "started"
STEP1_DONE = "step1_done"
STEP2_DONE = "step2_done"
COMPLETED = "completed"
COMPENSATING = "compensating"
FAILED = "failed"
class SagaLog(Base):
__tablename__ = "saga_log"
id: Mapped[int] = mapped_column(primary_key=True)
saga_id: Mapped[str] = mapped_column(String(36), unique=True)
status: Mapped[str] = mapped_column(String(50))
payload: Mapped[dict] = mapped_column(JSON)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
class OrderSaga:
def __init__(self, session: Session):
self.session = session
def execute(self, order_data: dict) -> str:
import uuid
saga_id = str(uuid.uuid4())
log = SagaLog(saga_id=saga_id, status=SagaStatus.STARTED, payload=order_data)
self.session.add(log)
self.session.commit()
try:
self._reserve_inventory(saga_id, order_data)
self._charge_payment(saga_id, order_data)
self._confirm_order(saga_id, order_data)
log.status = SagaStatus.COMPLETED
self.session.commit()
return saga_id
except Exception as e:
self._compensate(saga_id, log.status, order_data)
raise
def _compensate(self, saga_id: str, failed_at: str, data: dict):
"""Run compensating transactions in reverse order."""
if failed_at >= SagaStatus.STEP2_DONE:
self._refund_payment(data)
if failed_at >= SagaStatus.STEP1_DONE:
self._release_inventory(data)
🔍 Interviewer Notes: Saga pattern is a principal/staff engineer topic. Tests distributed systems design with real implementation experience.
❌ Common Mistakes:
- Not persisting saga state (can't resume after crash)
- Not implementing idempotent steps (retry runs compensating transaction twice)
- Using synchronous saga for I/O-bound distributed services (async orchestration is better)
Q229: How do you implement full-text search with ranking and highlighting?
Technical Answer:
Combine PostgreSQL's ts_rank_cd, ts_headline, and tsvector for relevance-ranked, highlighted full-text search results, all within SQLAlchemy's expression language.
Code Example:
from sqlalchemy import func, text
def full_text_search(session: Session, query: str, limit: int = 20):
ts_query = func.plainto_tsquery("english", query)
stmt = select(
Article.id,
Article.title,
func.ts_headline(
"english",
Article.body,
ts_query,
text("'MaxWords=50, MinWords=10, ShortWord=3, HighlightAll=FALSE'"),
).label("excerpt"),
func.ts_rank_cd(Article.search_vector, ts_query).label("rank"),
).where(
Article.search_vector.op("@@")(ts_query)
).order_by(
func.ts_rank_cd(Article.search_vector, ts_query).desc()
).limit(limit)
results = session.execute(stmt).all()
return [
{
"id": r.id,
"title": r.title,
"excerpt": r.excerpt, # contains <b>highlighted</b> terms
"score": float(r.rank),
}
for r in results
]
🔍 Interviewer Notes: ts_headline returns snippets with highlighted matches — a production-quality search feature in pure SQL/SQLAlchemy.
❌ Common Mistakes:
- Using
ts_rankinstead ofts_rank_cdfor documents (cd = cover density — better for text) - Computing highlights in Python (expensive — let the DB do it)
- Not maintaining
search_vectorvia trigger (stale vectors return wrong results)
Q230: How do you use MappedColumn with server_onupdate for DB-managed fields?
Technical Answer:
server_onupdate informs SQLAlchemy that a column's value is changed at the DB level on UPDATE (by a trigger or default). SQLAlchemy fetches the new value after the update if fetch_after_update=True.
Code Example:
from sqlalchemy.schema import FetchedValue
class Document(Base):
__tablename__ = "documents"
id: Mapped[int] = mapped_column(primary_key=True)
content: Mapped[str] = mapped_column(Text)
# Updated by a DB trigger — tell SQLAlchemy to refetch it
word_count: Mapped[Optional[int]] = mapped_column(
Integer,
server_default="0",
server_onupdate=FetchedValue(), # tells SQLAlchemy: refetch after UPDATE
)
version: Mapped[int] = mapped_column(
Integer,
server_default="1",
server_onupdate=FetchedValue(),
)
# After an UPDATE, SQLAlchemy knows to fetch word_count and version
with Session(engine) as session:
doc = session.get(Document, 1)
doc.content = "Updated content here..."
session.commit()
session.refresh(doc) # fetches DB-updated word_count and version
print(doc.word_count, doc.version)
🔍 Interviewer Notes: FetchedValue prevents SQLAlchemy from assuming the Python value is correct after a trigger-driven update.
❌ Common Mistakes:
- Not using
FetchedValueon trigger-managed columns (Python object shows stale value) - Not calling
session.refresh()after commit (even withFetchedValue— refresh is explicit) - Using
onupdate=func.now()when a DB trigger handles it (double update)
Q231: How do you implement connection-level prepared statements?
Technical Answer:
Prepared statements are compiled once and re-executed with different parameters, reducing per-query parse overhead. With psycopg2, use cursor.execute() with cursor_factory=NamedTupleCursor for named access.
Code Example:
import psycopg2
from sqlalchemy import event
# Using psycopg2 server-side prepared statements
@event.listens_for(engine, "connect")
def use_prepared_statements(dbapi_conn, conn_record):
# psycopg2 auto-prepares after threshold uses via `autocommit=False`
pass
# Direct psycopg2 prepared statement
with engine.connect() as conn:
# Get the underlying psycopg2 connection
dbapi_conn = conn.connection.dbapi_connection
cursor = dbapi_conn.cursor()
cursor.execute("PREPARE get_user AS SELECT * FROM users WHERE id = $1")
cursor.execute("EXECUTE get_user (%s)", (1,))
row = cursor.fetchone()
# SQLAlchemy compiled cache effectively achieves similar benefit
# without explicit PREPARE — same structure = same compiled query object
stmt = select(User).where(User.id == bindparam("user_id"))
with Session(engine) as session:
# First call: compiles, second call: uses cached compilation
user = session.scalar(stmt, {"user_id": 1})
user = session.scalar(stmt, {"user_id": 2}) # cached
🔍 Interviewer Notes: SQLAlchemy's compiled cache provides most benefits of server-side prepared statements transparently.
❌ Common Mistakes:
- Trying to set up explicit prepared statements in a pooled environment (prepared statements are connection-scoped — pool recycles connections)
- Not knowing SQLAlchemy's cache already provides the main benefit
- Using PgBouncer in transaction-mode pooling with server-side prepared statements (conflicts — use session mode)
Q232: How do you generate a schema diagram from SQLAlchemy models?
Technical Answer:
Use eralchemy2 or sqlalchemy_schemadisplay to auto-generate ER diagrams from MetaData. Useful for documentation and onboarding.
Code Example:
# pip install eralchemy2
from eralchemy2 import render_er
from myapp.models import Base
# Generate PNG from ORM models
render_er(Base, "schema.png")
# Generate from live DB
render_er("postgresql://user:pass@localhost/mydb", "schema_from_db.png")
# sqlalchemy_schemadisplay (text-based)
# pip install sqlalchemy_schemadisplay
from sqlalchemy_schemadisplay import create_schema_graph
from sqlalchemy import MetaData
graph = create_schema_graph(
metadata=Base.metadata,
show_datatypes=True,
show_indexes=True,
rankdir="LR",
concentrate=False,
)
graph.write_png("schema_detailed.png")
# In Jupyter
from IPython.display import Image
Image(graph.create_png())
🔍 Interviewer Notes: Auto-generated diagrams from code are always up-to-date — superior to manually maintained Lucidchart diagrams.
❌ Common Mistakes:
- Maintaining schema diagrams manually (they drift out of date)
- Not including diagrams in project documentation
- Generating from ORM models that haven't been migrated yet (diagram ≠ actual DB state)
Q233: How do you implement row-level encryption with transparent decryption?
Technical Answer:
Combine TypeDecorator + Fernet encryption for transparent field-level encryption. The encrypt/decrypt happens automatically on write/read, so application code doesn't handle raw ciphertext.
Code Example:
from cryptography.fernet import Fernet
from sqlalchemy.types import TypeDecorator, LargeBinary
import base64, os
class EncryptedString(TypeDecorator):
"""Transparent field-level encryption using Fernet."""
impl = LargeBinary
cache_ok = True
def __init__(self, key: bytes = None):
self._fernet = Fernet(key or os.environ["FIELD_ENCRYPTION_KEY"].encode())
super().__init__()
def process_bind_param(self, value, dialect):
if value is not None:
return self._fernet.encrypt(value.encode())
return None
def process_result_value(self, value, dialect):
if value is not None:
return self._fernet.decrypt(value).decode()
return None
class Patient(Base):
__tablename__ = "patients"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
ssn: Mapped[Optional[str]] = mapped_column(
EncryptedString(key=os.environ["FIELD_KEY"].encode()),
nullable=True,
)
diagnosis: Mapped[Optional[str]] = mapped_column(
EncryptedString(key=os.environ["FIELD_KEY"].encode()),
nullable=True,
)
🔍 Interviewer Notes: Field-level encryption is required for HIPAA/PCI-DSS compliance. The TypeDecorator approach keeps encryption logic in one place.
❌ Common Mistakes:
- Hardcoding the encryption key (must come from environment/secrets manager)
- Encrypting columns that need to be filtered/indexed (can't
WHERE ssn = ?on ciphertext) - Not rotating encryption keys (data locked to compromised key)
Q234: How do you implement a connection proxy for request tracing?
Technical Answer: Use SQLAlchemy connection events to inject trace IDs into every query via PostgreSQL application-level parameters. This makes queries traceable in DB logs and monitoring tools.
Code Example:
from sqlalchemy import event, text
from contextvars import ContextVar
import uuid
trace_id: ContextVar[str] = ContextVar("trace_id", default="")
@event.listens_for(engine, "checkout")
def set_trace_id(dbapi_conn, conn_record, conn_proxy):
current_trace = trace_id.get()
if current_trace:
cursor = dbapi_conn.cursor()
cursor.execute(
f"SET app.trace_id = '{current_trace}'"
)
cursor.execute(
f"SET application_name = 'myapp/{current_trace[:8]}'"
)
cursor.close()
# FastAPI middleware
@app.middleware("http")
async def add_trace_id(request: Request, call_next):
tid = request.headers.get("X-Trace-ID", str(uuid.uuid4()))
token = trace_id.set(tid)
try:
response = await call_next(request)
response.headers["X-Trace-ID"] = tid
finally:
trace_id.reset(token)
return response
# Query DB logs: SET application_name appears in pg_stat_activity
# SELECT pid, application_name, query FROM pg_stat_activity WHERE state = 'active'
🔍 Interviewer Notes: Trace ID in DB queries enables correlation between API logs and slow query logs. Tests observability maturity.
❌ Common Mistakes:
- Not using
ContextVar(global variable bleeds between concurrent requests) - Not sanitizing trace IDs before injecting into
SETcommand (SQL injection via trace ID) - Setting
application_nameto full trace ID (truncated by PostgreSQL at 64 chars)
Q235: How do you implement efficient hierarchical data queries using PostgreSQL ltree?
Technical Answer:
ltree is a PostgreSQL type for hierarchical data (org charts, categories, file paths). sqlalchemy_utils provides LtreeType for integration. Supports ancestor/descendant queries efficiently using GiST indexes.
Code Example:
# pip install sqlalchemy-utils psycopg2
from sqlalchemy_utils import LtreeType
from sqlalchemy import Index
class Category(Base):
__tablename__ = "categories"
__table_args__ = (
Index("idx_categories_path_gist", "path", postgresql_using="gist"),
)
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
path = mapped_column(LtreeType) # e.g., "electronics.phones.smartphones"
# Setup in migration
def upgrade():
op.execute("CREATE EXTENSION IF NOT EXISTS ltree")
op.add_column("categories", sa.Column("path", LtreeType()))
# Queries
with Session(engine) as session:
# Descendants of "electronics"
electronics_children = session.scalars(
select(Category).where(
Category.path.op("<@")(text("'electronics'::lquery"))
)
).all()
# Ancestors of a specific node
ancestors = session.scalars(
select(Category).where(
text("'electronics.phones.iphone' ~ path::text")
)
).all()
🔍 Interviewer Notes: ltree outperforms recursive CTEs for hierarchical data at scale. Tests PostgreSQL extension knowledge.
❌ Common Mistakes:
- Using recursive CTEs for deep hierarchies without ltree (slow on large trees)
- Not creating a GiST index (ltree queries without index do full table scan)
- Using path strings with spaces (ltree labels can't contain spaces — use underscores)
Q236: How do you implement a CDC (Change Data Capture) pattern with SQLAlchemy?
Technical Answer: CDC tracks all changes to tables and publishes them to a stream. SQLAlchemy can trigger CDC by combining session events with an outbox table or by listening to PostgreSQL WAL replication.
Code Example:
# Outbox pattern: changes written to outbox table within same transaction
class OutboxEvent(Base):
__tablename__ = "outbox_events"
id: Mapped[int] = mapped_column(primary_key=True)
aggregate_type: Mapped[str] = mapped_column(String(100))
aggregate_id: Mapped[int] = mapped_column(Integer)
event_type: Mapped[str] = mapped_column(String(100))
payload: Mapped[dict] = mapped_column(JSON)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
published_at: Mapped[Optional[datetime]] = mapped_column(default=None)
@event.listens_for(Session, "after_flush")
def emit_outbox_events(session, flush_context):
for obj in session.new:
if isinstance(obj, Order):
session.add(OutboxEvent(
aggregate_type="Order",
aggregate_id=obj.id,
event_type="OrderCreated",
payload={"order_id": obj.id, "total": float(obj.total)},
))
for obj in session.dirty:
if isinstance(obj, Order):
state = inspect(obj)
if state.attrs.status.history.has_changes():
session.add(OutboxEvent(
aggregate_type="Order",
aggregate_id=obj.id,
event_type="OrderStatusChanged",
payload={"new_status": obj.status},
))
🔍 Interviewer Notes: The transactional outbox pattern guarantees at-least-once delivery without distributed transactions. Tests event-driven architecture knowledge.
❌ Common Mistakes:
- Writing events to a message broker directly in the session event (not transactional — event sent but DB write fails)
- Using
after_commitfor outbox writes (too late — outbox must be in the same transaction as data) - Not implementing an outbox publisher worker (events accumulate forever)
Q237: How do you use SQLAlchemy with Pydantic v2 for model serialization?
Technical Answer:
Pydantic v2's model_validate(obj, from_attributes=True) reads SQLAlchemy ORM attributes. Configure with model_config = ConfigDict(from_attributes=True). Always convert inside the session to avoid DetachedInstanceError.
Code Example:
from pydantic import BaseModel, ConfigDict
from typing import Optional, List
from datetime import datetime
class PostSchema(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
title: str
created_at: datetime
author: Optional["UserSchema"] = None
class UserSchema(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
name: str
email: str
posts: List[PostSchema] = []
# FastAPI usage — serialize INSIDE session context
@router.get("/users/{id}", response_model=UserSchema)
async def get_user(id: int, session: AsyncSession = Depends(get_db)):
user = await session.scalar(
select(User)
.options(selectinload(User.posts))
.where(User.id == id)
)
if not user:
raise HTTPException(404)
# Serialize while session is open
return UserSchema.model_validate(user)
# Batch serialization
users = session.scalars(
select(User).options(selectinload(User.posts))
).all()
schemas = [UserSchema.model_validate(u) for u in users]
🔍 Interviewer Notes: from_attributes=True replaces Pydantic v1's orm_mode = True. Serializing inside the session prevents DetachedInstanceError.
❌ Common Mistakes:
- Using
orm_mode = True(Pydantic v1 syntax — doesn't work in v2) - Serializing after session closes (lazy-load triggers
DetachedInstanceError) - Not eager-loading nested relationships before serialization (N+1 inside
model_validate)
Q238: How do you implement a connection string rotation without downtime?
Technical Answer:
Connection string rotation (credential rotation) can be done zero-downtime by: updating the secret in the secrets manager, using pool_pre_ping=True to detect stale connections, and triggering pool refresh via engine.dispose().
Code Example:
import threading, time
class RotatingEngine:
def __init__(self):
self._engine = None
self._lock = threading.Lock()
self._create_engine()
self._start_rotation_watcher()
def _create_engine(self):
url = get_secret_from_aws_secrets_manager("db/credentials")
self._engine = create_engine(url, pool_pre_ping=True)
def _start_rotation_watcher(self):
def watch():
last_version = get_secret_version("db/credentials")
while True:
time.sleep(30)
current_version = get_secret_version("db/credentials")
if current_version != last_version:
with self._lock:
self._engine.dispose() # close all pooled connections
self._create_engine() # new engine with new credentials
last_version = current_version
t = threading.Thread(target=watch, daemon=True)
t.start()
@property
def engine(self):
with self._lock:
return self._engine
🔍 Interviewer Notes: Zero-downtime credential rotation is a production ops requirement for compliance. Tests operational maturity.
❌ Common Mistakes:
- Not calling
engine.dispose()on rotation (old connections still use expired credentials) - Not using
pool_pre_ping=True(stale connections fail before rotation is detected) - Rotating credentials without a grace period (old + new credentials valid simultaneously)
Q239: How do you implement a query timeout at the SQLAlchemy level?
Technical Answer:
Set statement timeouts at the connection level via pool checkout events, or per-query via execution_options. PostgreSQL uses statement_timeout; MySQL uses MAX_EXECUTION_TIME.
Code Example:
from sqlalchemy import event, text
# Global: all connections get 30s timeout
@event.listens_for(engine, "checkout")
def set_statement_timeout(dbapi_conn, conn_record, conn_proxy):
cursor = dbapi_conn.cursor()
cursor.execute("SET statement_timeout = '30s'")
cursor.close()
# Per-query timeout via execution_options
with Session(engine) as session:
result = session.execute(
select(User).execution_options(
postgresql_statement_timeout="5000" # 5 seconds in ms
)
)
# Async version
async with AsyncSession(async_engine) as session:
await session.execute(
text("SET LOCAL statement_timeout = '5s'")
)
result = await session.scalars(select(User))
# Application-level timeout (asyncio)
import asyncio
async def query_with_timeout(session, stmt, timeout_secs=10):
try:
return await asyncio.wait_for(
session.scalars(stmt),
timeout=timeout_secs,
)
except asyncio.TimeoutError:
await session.rollback()
raise
🔍 Interviewer Notes: DB-level timeouts prevent runaway queries from holding connections. Tests production hardening knowledge.
❌ Common Mistakes:
- Only setting timeouts in the application (doesn't protect the DB from runaway queries)
- Not resetting
statement_timeoutbetween requests (previous request's timeout applies) - Using
lock_timeoutinstead ofstatement_timeout(different —lock_timeoutonly applies to lock acquisition)
Q240: How do you implement a global before_flush data validation pipeline?
Technical Answer:
Register a before_flush session event that runs all pending objects through a validation registry. Each model class can declare validators that run before any data reaches the DB.
Code Example:
from typing import Callable
from sqlalchemy import event
from sqlalchemy.orm import Session
_validators: dict[type, list[Callable]] = {}
def register_validator(model_class):
def decorator(func):
_validators.setdefault(model_class, []).append(func)
return func
return decorator
@event.listens_for(Session, "before_flush")
def run_validators(session, flush_context, instances):
for obj in list(session.new) + list(session.dirty):
for validator_fn in _validators.get(type(obj), []):
validator_fn(obj, session)
# Define validators
@register_validator(User)
def validate_email_domain(user, session):
blocked = ["spam.com", "temp.com"]
domain = user.email.split("@")[-1]
if domain in blocked:
raise ValueError(f"Email domain {domain} is not allowed")
@register_validator(Order)
def validate_order_total(order, session):
if order.total <= 0:
raise ValueError("Order total must be positive")
if order.total > 100000:
raise ValueError("Order total exceeds maximum allowed")
# All User and Order objects validated automatically before flush
with Session(engine) as session:
user = User(name="Alice", email="alice@spam.com")
session.add(user)
session.commit() # raises ValueError before reaching DB
🔍 Interviewer Notes: Centralized validation pipeline prevents duplicate validation logic across services. Tests ORM design thinking.
❌ Common Mistakes:
- Running validators in
after_flush(too late — data already sent to DB) - Doing DB queries inside validators without
session.no_autoflush(recursive flush) - Not propagating validation errors clearly (catching at the session level loses context)
Q241: How do you implement connection pool warming at startup?
Technical Answer:
Pool warming pre-creates all connections at startup, avoiding cold start latency on the first batch of requests. Use engine.execute("SELECT 1") N times (N = pool_size) before accepting traffic.
Code Example:
from sqlalchemy import text, create_engine
from concurrent.futures import ThreadPoolExecutor
engine = create_engine(
DATABASE_URL,
pool_size=20,
max_overflow=5,
pool_pre_ping=True,
)
def warm_pool(engine, count: int = None):
"""Pre-create pool connections to eliminate cold-start latency."""
count = count or engine.pool.size()
connections = []
try:
for _ in range(count):
conn = engine.connect()
conn.execute(text("SELECT 1"))
connections.append(conn)
finally:
for conn in connections:
conn.close() # returns to pool (doesn't close underlying connection)
print(f"Pool warmed: {engine.pool.checkedin()} connections ready")
# FastAPI startup event
@app.on_event("startup")
async def startup():
import asyncio
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, warm_pool, engine)
🔍 Interviewer Notes: Pool warming matters for autoscaled services where new instances see cold start spikes on traffic ramp-up.
❌ Common Mistakes:
- Not warming the pool in container-based deployments (each new pod gets cold start)
- Using
pool_pre_ping=Truethinking it warms the pool (it only checks health on checkout — doesn't pre-create) - Warming more connections than
pool_size(extra ones immediately returned to overflow — wasted)
Q242: How do you implement a multi-model transaction spanning repositories?
Technical Answer:
Pass a single Session to all repositories involved in one use case. The session acts as the Unit of Work — commit once at the end to atomically persist all changes across repositories.
Code Example:
class UserRepository:
def __init__(self, session: Session): self.session = session
def create(self, **kwargs): ...
def get(self, id): return self.session.get(User, id)
class WalletRepository:
def __init__(self, session: Session): self.session = session
def create_for_user(self, user): ...
def debit(self, wallet_id, amount): ...
class NotificationRepository:
def __init__(self, session: Session): self.session = session
def create(self, **kwargs): ...
def register_user_use_case(session: Session, name: str, email: str):
"""All operations in a single atomic transaction."""
user_repo = UserRepository(session)
wallet_repo = WalletRepository(session)
notification_repo = NotificationRepository(session)
user = user_repo.create(name=name, email=email)
wallet = wallet_repo.create_for_user(user)
notification_repo.create(
user_id=user.id,
message=f"Welcome {name}! Your wallet is ready.",
type="welcome",
)
# All three created atomically — if any fails, all roll back
session.commit()
return user
# FastAPI usage
@app.post("/register")
async def register(data: RegisterRequest, session: AsyncSession = Depends(get_db)):
return await asyncio.get_event_loop().run_in_executor(
None, register_user_use_case, session, data.name, data.email
)
🔍 Interviewer Notes: Shared session across repositories = atomic multi-entity operations. This is the standard pattern for DDD + SQLAlchemy.
❌ Common Mistakes:
- Creating a new session per repository (each operates in its own transaction — no atomicity)
- Committing inside repositories (breaks caller's ability to control transaction boundaries)
- Not passing session via dependency injection (hard to test without a real DB)
Q243: How do you implement a streaming export to CSV with SQLAlchemy?
Technical Answer:
Use yield_per + server-side cursors to stream rows and write to CSV in chunks, without loading the full result into memory. Combine with StreamingResponse in FastAPI for HTTP streaming.
Code Example:
import csv, io
from fastapi.responses import StreamingResponse
def generate_csv(session: Session, stmt):
"""Generator that yields CSV rows in chunks."""
# Header row
buf = io.StringIO()
writer = csv.writer(buf)
writer.writerow(["id", "name", "email", "created_at"])
yield buf.getvalue()
buf.seek(0); buf.truncate()
# Data rows in chunks
result = session.execute(
stmt.execution_options(stream_results=True, yield_per=1000)
)
for partition in result.partitions(1000):
for row in partition:
writer.writerow([row.id, row.name, row.email, row.created_at.isoformat()])
yield buf.getvalue()
buf.seek(0); buf.truncate()
# FastAPI streaming endpoint
@app.get("/export/users")
def export_users(session: Session = Depends(get_db)):
stmt = select(User.id, User.name, User.email, User.created_at).order_by(User.id)
return StreamingResponse(
generate_csv(session, stmt),
media_type="text/csv",
headers={"Content-Disposition": "attachment; filename=users.csv"},
)
🔍 Interviewer Notes: Streaming exports are essential for large datasets (millions of rows). Tests full-stack async/streaming knowledge.
❌ Common Mistakes:
- Loading all rows into memory before writing CSV (OOM for large tables)
- Not using
yield_perwith server-side cursors (still fetches all rows to client) - Not flushing the StringIO buffer between chunks (yields only partial rows)
Q244: How do you use connection.info for per-connection state?
Technical Answer:
connection.info is a per-connection dictionary that persists for the lifetime of the connection (not just the session). Useful for storing metadata that should survive transaction boundaries.
Code Example:
from sqlalchemy import event
from sqlalchemy.engine import Connection
@event.listens_for(engine, "connect")
def store_pid(dbapi_conn, conn_record):
conn_record.info["pid"] = dbapi_conn.get_backend_pid() # psycopg2-specific
@event.listens_for(engine, "checkout")
def log_checkout(dbapi_conn, conn_record, conn_proxy):
conn_record.info.setdefault("checkout_count", 0)
conn_record.info["checkout_count"] += 1
if conn_record.info["checkout_count"] % 100 == 0:
print(f"Connection {conn_record.info.get('pid')} checked out 100 times")
# Connection-level caching (persists across transactions in same connection)
@event.listens_for(Connection, "after_execute")
def cache_result(conn, clauseelement, multiparams, params, execution_options, result):
if hasattr(clauseelement, "_cache_key"):
conn.info[f"cache:{clauseelement._cache_key}"] = result.fetchall()
🔍 Interviewer Notes: conn_record.info is the correct location for connection-scoped state (vs session-scoped which resets per request).
❌ Common Mistakes:
- Using
session.infowhen you need connection-level persistence - Storing large objects in
conn_record.info(persists in pool — memory leak) - Confusing
conn_record.info(pool-level) withconnection.info(engine Connection object)
Q245: How do you implement a generic upsert for all models?
Technical Answer:
A generic upsert function uses the model's __table__ and primary key columns to construct INSERT ... ON CONFLICT DO UPDATE for any model. Requires PostgreSQL dialect.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy import inspect as sa_inspect
def upsert(session: Session, model_class, data: list[dict]) -> int:
"""Generic upsert for any SQLAlchemy model."""
if not data:
return 0
mapper = sa_inspect(model_class)
pk_cols = [col.key for col in mapper.primary_key]
table = model_class.__table__
stmt = pg_insert(table).values(data)
# All non-PK columns go in the update set
update_cols = {
col.name: stmt.excluded[col.name]
for col in table.columns
if col.name not in pk_cols
}
stmt = stmt.on_conflict_do_update(
index_elements=pk_cols,
set_=update_cols,
)
result = session.execute(stmt)
return result.rowcount
# Usage
with Session(engine) as session:
rows_affected = upsert(session, User, [
{"id": 1, "name": "Alice Updated", "email": "alice@example.com"},
{"id": 99, "name": "New User", "email": "new@example.com"},
])
session.commit()
🔍 Interviewer Notes: Generic upsert is essential for data pipelines that sync from external sources. Tests meta-programming + SQLAlchemy introspection.
❌ Common Mistakes:
- Hard-coding model columns in the
set_=dict (breaks when columns change) - Including PK columns in the update set (overwrites PK — usually wrong)
- Using
session.merge()for concurrent upserts (race condition vs atomicON CONFLICT)
Q246: How does SQLAlchemy 2.0 handle Mapped with ClassVar?
Technical Answer:
ClassVar[T] annotations are excluded from SQLAlchemy's mapper. Use it for class-level attributes that should NOT become columns or relationships.
Code Example:
from typing import ClassVar
from sqlalchemy.orm import Mapped, mapped_column
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# ClassVar — NOT a column, not part of mapper
VALID_ROLES: ClassVar[list[str]] = ["admin", "user", "moderator"]
MAX_NAME_LENGTH: ClassVar[int] = 100
# This is a column (no ClassVar)
role: Mapped[str] = mapped_column(String(50), default="user")
@classmethod
def validate_role(cls, role: str) -> bool:
return role in cls.VALID_ROLES
# SQLAlchemy ignores VALID_ROLES and MAX_NAME_LENGTH
mapper = sa_inspect(User)
print([c.key for c in mapper.columns])
# ['id', 'name', 'role'] — ClassVar excluded
🔍 Interviewer Notes: Without ClassVar, SQLAlchemy 2.0 may try to map unexpected class attributes as columns. Tests annotation precision.
❌ Common Mistakes:
- Not using
ClassVarfor class-level constants (SQLAlchemy may warn about unexpected mapped attributes) - Using
ClassVaron methods (methods are already excluded — only needed for data attributes) - Confusing
ClassVar(excluded from mapper) with@declared_attr(evaluated per subclass)
Q247: How do you implement database-level full-text indexing in an Alembic migration?
Technical Answer:
Create a GIN-indexed tsvector column via op.add_column(), populate it from existing data, and create a trigger to keep it updated. All within a single idempotent Alembic migration.
Code Example:
def upgrade():
# Enable extension
op.execute("CREATE EXTENSION IF NOT EXISTS unaccent")
# Add tsvector column
op.add_column("articles", sa.Column("search_vector", postgresql.TSVECTOR, nullable=True))
# Create GIN index
op.create_index(
"idx_articles_search_vector",
"articles",
["search_vector"],
postgresql_using="gin",
)
# Populate existing rows
op.execute("""
UPDATE articles SET search_vector =
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'B')
""")
# Create trigger to maintain on INSERT/UPDATE
op.execute("""
CREATE OR REPLACE FUNCTION articles_search_vector_update() RETURNS trigger AS $$
BEGIN
NEW.search_vector :=
setweight(to_tsvector('english', coalesce(NEW.title,'')), 'A') ||
setweight(to_tsvector('english', coalesce(NEW.body,'')), 'B');
RETURN NEW;
END
$$ LANGUAGE plpgsql;
""")
op.execute("""
CREATE TRIGGER articles_search_vector_update_trigger
BEFORE INSERT OR UPDATE OF title, body ON articles
FOR EACH ROW EXECUTE FUNCTION articles_search_vector_update()
""")
def downgrade():
op.execute("DROP TRIGGER IF EXISTS articles_search_vector_update_trigger ON articles")
op.execute("DROP FUNCTION IF EXISTS articles_search_vector_update()")
op.drop_index("idx_articles_search_vector", table_name="articles")
op.drop_column("articles", "search_vector")
🔍 Interviewer Notes: Production full-text search requires: GIN index + tsvector column + update trigger. All three must be in migrations.
❌ Common Mistakes:
- Not creating the update trigger (search_vector stale after row updates)
- Not backfilling existing rows (existing data unsearchable)
- Creating GIN index before backfill (index built twice — build after populate)
Q248: How do you implement a priority queue pattern using SQLAlchemy?
Technical Answer:
A DB-backed priority queue uses SELECT ... FOR UPDATE SKIP LOCKED to safely dequeue items without row-level conflicts, allowing multiple workers to process concurrently without blocking each other.
Code Example:
from sqlalchemy import select, update
from datetime import datetime
class Job(Base):
__tablename__ = "jobs"
__table_args__ = (
Index("idx_jobs_priority_created", "priority", "created_at"),
)
id: Mapped[int] = mapped_column(primary_key=True)
payload: Mapped[dict] = mapped_column(JSON)
priority: Mapped[int] = mapped_column(Integer, default=5) # 1=highest
status: Mapped[str] = mapped_column(String(20), default="pending")
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
locked_at: Mapped[Optional[datetime]] = mapped_column(default=None)
def dequeue(session: Session) -> Optional[Job]:
"""Dequeue next job safely — multiple workers can call concurrently."""
job = session.scalar(
select(Job)
.where(Job.status == "pending")
.order_by(Job.priority, Job.created_at)
.limit(1)
.with_for_update(skip_locked=True) # KEY: skip rows locked by other workers
)
if job:
job.status = "processing"
job.locked_at = datetime.utcnow()
session.commit()
return job
# Worker
def worker_loop():
with Session(engine) as session:
while True:
job = dequeue(session)
if not job:
time.sleep(1)
continue
try:
process(job.payload)
job.status = "completed"
except Exception as e:
job.status = "failed"
session.commit()
🔍 Interviewer Notes: SKIP LOCKED is the PostgreSQL pattern for job queues. Tests concurrency + SQL knowledge.
❌ Common Mistakes:
- Using
FOR UPDATEwithoutSKIP LOCKED(worker 2 blocks waiting for worker 1's lock) - Not indexing
(priority, created_at)(full table scan per dequeue) - Not handling worker crashes (jobs stuck in "processing" — add
locked_attimeout cleanup)
Q249: How do you implement multi-level caching with SQLAlchemy?
Technical Answer: Multi-level caching combines SQLAlchemy's identity map (L1), application cache (L2 via Redis), and DB query cache (L3 via materialized views). Each level has different TTL and granularity.
Code Example:
from functools import lru_cache
import redis, json
redis_client = redis.Redis(host="localhost")
class CachedUserService:
def __init__(self, session: Session):
self.session = session
def get_user(self, user_id: int) -> Optional[User]:
# L1: SQLAlchemy identity map (in-session cache)
cached = self.session.identity_map.get((User, (user_id,)))
if cached:
return cached
# L2: Redis application cache
cache_key = f"user:{user_id}"
cached_json = redis_client.get(cache_key)
if cached_json:
data = json.loads(cached_json)
# Merge into session without hitting DB
return self.session.merge(User(**data))
# L3: Database
user = self.session.get(User, user_id)
if user:
redis_client.setex(
cache_key, 300,
json.dumps({"id": user.id, "name": user.name, "email": user.email})
)
return user
def invalidate_user(self, user_id: int):
redis_client.delete(f"user:{user_id}")
# L1 (identity map) invalidated automatically by session.expire()
🔍 Interviewer Notes: Multi-level caching shows architectural maturity. Tests tradeoff awareness between consistency and performance.
❌ Common Mistakes:
- Using
session.merge()with cached data without understanding it may trigger a SELECT (if PK not in identity map) - Not invalidating cache on writes (stale data returned)
- Caching relationships (invalidation complexity multiplies)
Q250: How do you implement a complete async SQLAlchemy FastAPI integration?
Technical Answer:
Full async integration requires: create_async_engine + async_sessionmaker + AsyncSession as FastAPI dependency + eager loading in all queries + Pydantic serialization inside session.
Code Example:
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase, selectinload
from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel, ConfigDict
# Setup
async_engine = create_async_engine(
"postgresql+asyncpg://user:pass@localhost/mydb",
pool_size=20, pool_pre_ping=True, echo=False,
)
AsyncSessionLocal = async_sessionmaker(async_engine, expire_on_commit=False)
# Dependency
async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
yield session
# Schema
class UserOut(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int; name: str; email: str
class PostOut(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int; title: str
author: UserOut
# Endpoints
@app.get("/posts/{id}", response_model=PostOut)
async def get_post(id: int, session: AsyncSession = Depends(get_db)):
post = await session.scalar(
select(Post)
.options(joinedload(Post.author))
.where(Post.id == id)
)
if not post:
raise HTTPException(404, "Post not found")
return PostOut.model_validate(post)
@app.post("/posts", response_model=PostOut)
async def create_post(data: PostCreate, session: AsyncSession = Depends(get_db)):
post = Post(title=data.title, author_id=data.author_id)
session.add(post)
await session.flush() # get ID without commit
await session.refresh(post)
result = PostOut.model_validate(post)
await session.commit()
return result
# Startup
@app.on_event("startup")
async def startup():
async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
🔍 Interviewer Notes: This is the complete production-grade async FastAPI + SQLAlchemy pattern. Tests end-to-end integration knowledge.
❌ Common Mistakes:
- Not using
expire_on_commit=Falsein async (triggers unexpected lazy load coroutines after commit) - Returning ORM objects from endpoints without eager loading (lazy load outside session context)
- Not using
async_sessionmaker(usingAsyncSession(engine)directly skips proper factory configuration)
Q251: How do you implement a generic query builder with type safety?
Technical Answer:
Build a QueryBuilder class that accumulates filters, sorts, and pagination options using method chaining. Use TypeVar to maintain return type consistency. This enables reusable filtering logic across endpoints.
Code Example:
from typing import TypeVar, Generic, Type, List, Optional
from sqlalchemy.orm import Session
from sqlalchemy import select, asc, desc
T = TypeVar("T")
class QueryBuilder(Generic[T]):
def __init__(self, session: Session, model: Type[T]):
self._session = session
self._model = model
self._filters = []
self._order_by = []
self._limit: Optional[int] = None
self._offset: Optional[int] = None
self._options = []
def filter(self, *conditions) -> "QueryBuilder[T]":
self._filters.extend(conditions)
return self
def order_by(self, *columns) -> "QueryBuilder[T]":
self._order_by.extend(columns)
return self
def paginate(self, page: int = 1, per_page: int = 20) -> "QueryBuilder[T]":
self._limit = per_page
self._offset = (page - 1) * per_page
return self
def with_options(self, *options) -> "QueryBuilder[T]":
self._options.extend(options)
return self
def all(self) -> List[T]:
stmt = select(self._model)
for f in self._filters:
stmt = stmt.where(f)
for o in self._order_by:
stmt = stmt.order_by(o)
if self._options:
stmt = stmt.options(*self._options)
if self._limit:
stmt = stmt.limit(self._limit)
if self._offset:
stmt = stmt.offset(self._offset)
return self._session.scalars(stmt).all()
def count(self) -> int:
from sqlalchemy import func
stmt = select(func.count()).select_from(self._model)
for f in self._filters:
stmt = stmt.where(f)
return self._session.scalar(stmt)
# Usage
users = (
QueryBuilder(session, User)
.filter(User.is_active == True, User.created_at > cutoff)
.order_by(User.name.asc())
.with_options(selectinload(User.posts))
.paginate(page=2, per_page=20)
.all()
)
🔍 Interviewer Notes: Tests design patterns and type-safe API construction.
❌ Common Mistakes:
- Not returning
selffrom chained methods (breaks chaining) - Not separating count from data queries (for pagination metadata)
- Building SQL via string concatenation instead of SQLAlchemy expression API
Q252: How does SQLAlchemy 2.0 handle Mapped type with Optional vs non-optional?
Technical Answer:
Mapped[str] generates a NOT NULL column. Mapped[Optional[str]] generates a nullable column. This inference happens at class creation time via get_type_hints(). The column nullability is derived automatically from the Python type annotation.
Code Example:
from typing import Optional
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase
from sqlalchemy import String
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
# NOT NULL — required field
name: Mapped[str] = mapped_column(String(100))
# Nullable — optional field
bio: Mapped[Optional[str]] = mapped_column(String(500), default=None)
# Nullable with server default
avatar_url: Mapped[Optional[str]] = mapped_column(String(500), server_default=None)
# Override inference: make non-optional type nullable
legacy_code: Mapped[str] = mapped_column(String(20), nullable=True) # overrides
# Verify generated DDL
from sqlalchemy.schema import CreateTable
from sqlalchemy.dialects import postgresql
print(CreateTable(User.__table__).compile(dialect=postgresql.dialect()))
# Output:
# CREATE TABLE users (
# id SERIAL NOT NULL,
# name VARCHAR(100) NOT NULL,
# bio VARCHAR(500), <- nullable
# avatar_url VARCHAR(500), <- nullable
# legacy_code VARCHAR(20), <- nullable (explicit override)
# PRIMARY KEY (id)
# )
🔍 Interviewer Notes: Tests understanding of SQLAlchemy 2.0 annotation inference system.
❌ Common Mistakes:
- Using
Optional[str] = mapped_column(nullable=False)(annotation says nullable, explicit says not — explicit wins) - Forgetting
Optionalon fields that should allow NULL - Using
Union[str, None]instead ofOptional[str](both work in 2.0 butOptionalis cleaner)
Q253: How do you implement a read-through cache pattern with SQLAlchemy?
Technical Answer: Read-through cache: check Redis first, on miss fetch from DB and populate cache. Write-through: write to both DB and cache atomically. Use cache-aside for simpler implementations. Key by model type + PK.
Code Example:
import json
import redis
from sqlalchemy.orm import Session
class CachedRepository:
def __init__(self, session: Session, cache: redis.Redis, ttl: int = 300):
self._session = session
self._cache = cache
self._ttl = ttl
def _cache_key(self, model_cls, pk) -> str:
return f"{model_cls.__tablename__}:{pk}"
def get_by_id(self, model_cls, pk: int):
key = self._cache_key(model_cls, pk)
# Check cache first
cached = self._cache.get(key)
if cached:
data = json.loads(cached)
return data # Or reconstruct model instance
# Cache miss: fetch from DB
obj = self._session.get(model_cls, pk)
if obj is None:
return None
# Populate cache (serialize to dict)
data = {c.name: getattr(obj, c.name) for c in obj.__table__.columns}
self._cache.setex(key, self._ttl, json.dumps(data, default=str))
return obj
def save(self, obj):
"""Write-through: update both DB and cache."""
self._session.add(obj)
self._session.flush()
key = self._cache_key(type(obj), obj.id)
data = {c.name: getattr(obj, c.name) for c in obj.__table__.columns}
self._cache.setex(key, self._ttl, json.dumps(data, default=str))
def invalidate(self, model_cls, pk: int):
"""Cache invalidation on delete."""
key = self._cache_key(model_cls, pk)
self._cache.delete(key)
🔍 Interviewer Notes: Tests caching patterns and cache-database consistency strategies.
❌ Common Mistakes:
- Not invalidating cache on updates (stale reads)
- Using the same TTL for all entity types (high-churn data needs shorter TTL)
- Not handling Redis connection failures gracefully (fall through to DB)
Q254: How do you implement a database-level locking strategy for inventory management?
Technical Answer:
Use SELECT ... FOR UPDATE (pessimistic) for high-contention stock reservations. For lower-contention, use optimistic locking with version_id_col. For bulk operations, use UPDATE inventory SET quantity = quantity - :qty WHERE quantity >= :qty RETURNING quantity.
Code Example:
from sqlalchemy import text, select
from sqlalchemy.exc import StaleDataError
from sqlalchemy.dialects.postgresql import insert as pg_insert
class Inventory(Base):
__tablename__ = "inventory"
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"), primary_key=True)
quantity: Mapped[int] = mapped_column(default=0)
reserved: Mapped[int] = mapped_column(default=0)
version_id: Mapped[int] = mapped_column(default=1)
__mapper_args__ = {"version_id_col": version_id}
# Pessimistic: FOR UPDATE (flash sales)
def reserve_stock_pessimistic(session: Session, product_id: int, qty: int) -> bool:
inv = session.scalars(
select(Inventory)
.where(Inventory.product_id == product_id)
.with_for_update() # Row-level exclusive lock
).one_or_none()
if not inv or inv.quantity - inv.reserved < qty:
return False
inv.reserved += qty
session.commit()
return True
# Atomic update approach (best performance)
def deduct_stock_atomic(session: Session, product_id: int, qty: int) -> bool:
result = session.execute(
text("""
UPDATE inventory
SET quantity = quantity - :qty
WHERE product_id = :pid AND quantity >= :qty
RETURNING quantity
"""),
{"qty": qty, "pid": product_id}
).fetchone()
if result is None:
session.rollback()
return False
session.commit()
return True
🔍 Interviewer Notes: Tests knowledge of concurrency control for high-stakes business operations.
❌ Common Mistakes:
- Using optimistic locking for flash sales (too many conflicts under high contention)
- Not using
RETURNINGto verify the update happened - Not releasing locks promptly (holding FOR UPDATE lock while calling external payment API)
Q255: How do you implement a plugin architecture with SQLAlchemy models?
Technical Answer:
Use declared_attr with a base class to define shared behavior, and let plugins register their own models by importing Base. SQLAlchemy's mapper registry collects all models automatically at create_all() time.
Code Example:
from sqlalchemy.orm import DeclarativeBase, declared_attr, Mapped, mapped_column
from sqlalchemy import String, DateTime
from datetime import datetime
class Base(DeclarativeBase):
pass
class TimestampMixin:
@declared_attr
def created_at(cls) -> Mapped[datetime]:
return mapped_column(DateTime, default=datetime.utcnow)
@declared_attr
def updated_at(cls) -> Mapped[datetime]:
return mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
class SoftDeleteMixin:
@declared_attr
def deleted_at(cls) -> Mapped[Optional[datetime]]:
return mapped_column(DateTime, nullable=True, default=None)
# Core models
class User(TimestampMixin, Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
# Plugin: billing module registers its own models against the same Base
# billing/models.py
from myapp.database import Base # Same Base instance
class Invoice(TimestampMixin, SoftDeleteMixin, Base):
__tablename__ = "invoices"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
amount: Mapped[float] = mapped_column()
# All models auto-registered when billing.models is imported
# app.py
import billing.models # Side effect: registers Invoice with Base.metadata
Base.metadata.create_all(engine) # Creates users + invoices
🔍 Interviewer Notes: Tests modular architecture patterns with SQLAlchemy.
❌ Common Mistakes:
- Using multiple
Baseclasses across plugins (tables not in the same metadata) - Not importing plugin models before
create_all()(tables missing) - Circular imports between plugin models and core models
Q256: How do you implement a temporal data pattern (bi-temporal) with SQLAlchemy?
Technical Answer:
Bi-temporal tables store two time dimensions: valid_time (when the fact is true in the real world) and transaction_time (when the record was stored). Use tstzrange for each dimension with no-overlap constraints.
Code Example:
from sqlalchemy.dialects.postgresql import TSTZRANGE
from sqlalchemy import Column, Index
class EmployeeSalary(Base):
__tablename__ = "employee_salaries"
id: Mapped[int] = mapped_column(primary_key=True)
employee_id: Mapped[int] = mapped_column(ForeignKey("employees.id"))
salary: Mapped[float] = mapped_column()
# When this salary is valid in the real world
valid_period = Column(TSTZRANGE, nullable=False)
# When this record was known to the system
transaction_period = Column(TSTZRANGE, nullable=False,
server_default=text("tstzrange(now(), 'infinity')"))
__table_args__ = (
# No overlapping valid periods for same employee
# (requires btree_gist extension)
Index("ix_salary_employee_valid", "employee_id", "valid_period",
postgresql_using="gist"),
)
# Query: what was the salary on a specific date?
def get_salary_at(session, employee_id: int, at_date: datetime) -> Optional[float]:
result = session.scalars(
select(EmployeeSalary.salary)
.where(
EmployeeSalary.employee_id == employee_id,
EmployeeSalary.valid_period.op("@>")(at_date), # contains operator
EmployeeSalary.transaction_period.op("@>")(datetime.now(timezone.utc)),
)
).first()
return result
# Correct a historical record without losing audit trail
def correct_salary(session, emp_id: int, wrong_period, corrected_salary: float):
# Close transaction_time on old record
session.execute(
text("""UPDATE employee_salaries
SET transaction_period = tstzrange(lower(transaction_period), now())
WHERE employee_id = :eid AND valid_period = :vp
AND upper(transaction_period) = 'infinity'"""),
{"eid": emp_id, "vp": wrong_period}
)
# Insert corrected record
session.add(EmployeeSalary(
employee_id=emp_id, salary=corrected_salary, valid_period=wrong_period
))
🔍 Interviewer Notes: Tests knowledge of temporal data modeling for audit/compliance requirements.
❌ Common Mistakes:
- Using only transaction_time (can't represent "when it was true in reality")
- Not using range types (separate start/end columns miss range operators)
- Modifying historical records instead of appending corrections
Q257: How do you implement a notification system with database-backed queuing?
Technical Answer:
Store notifications in a notifications table. Use SKIP LOCKED for multi-worker consumption without a message broker. Mark notifications as delivered with a timestamp to avoid reprocessing.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
class Notification(Base):
__tablename__ = "notifications"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
type: Mapped[str] = mapped_column(String(50))
payload: Mapped[dict] = mapped_column(JSON)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
read_at: Mapped[Optional[datetime]] = mapped_column(default=None)
delivered_at: Mapped[Optional[datetime]] = mapped_column(default=None)
__table_args__ = (
Index("ix_notifications_user_unread", "user_id",
postgresql_where=(read_at.is_(None))),
)
def get_unread(session: Session, user_id: int, limit: int = 50) -> list:
return session.scalars(
select(Notification)
.where(Notification.user_id == user_id, Notification.read_at.is_(None))
.order_by(Notification.created_at.desc())
.limit(limit)
).all()
def mark_read(session: Session, notification_ids: list[int], user_id: int):
session.execute(
Notification.__table__.update()
.where(
Notification.id.in_(notification_ids),
Notification.user_id == user_id,
)
.values(read_at=datetime.utcnow())
)
session.commit()
# Worker: deliver notifications via email/push
def deliver_pending(session: Session, batch_size: int = 100):
notifications = session.scalars(
select(Notification)
.where(Notification.delivered_at.is_(None))
.order_by(Notification.created_at)
.limit(batch_size)
.with_for_update(skip_locked=True)
).all()
for notif in notifications:
send_push(notif)
notif.delivered_at = datetime.utcnow()
session.commit()
🔍 Interviewer Notes: Tests database-backed queue patterns and delivery guarantee strategies.
❌ Common Mistakes:
- Not using
SKIP LOCKED(multiple workers deliver same notification) - Not separating read status from delivery status
- Not adding partial index on unread notifications (full scan for each user)
Q258: How do you implement schema validation before applying Alembic migrations?
Technical Answer:
Use alembic check (Alembic 1.9+) in CI to verify the model schema matches the database. Alternatively, write a custom comparison using Inspector and the model's MetaData to detect drift before deployment.
Code Example:
# CI step: check for schema drift
# alembic check # Exits non-zero if migrations are pending
# Custom drift detection
from sqlalchemy import create_engine, inspect, MetaData
from alembic.config import Config
from alembic.runtime.migration import MigrationContext
from alembic.autogenerate import compare_metadata
def check_schema_drift(database_url: str) -> list[str]:
engine = create_engine(database_url)
mc = MigrationContext.configure(engine.connect())
diffs = compare_metadata(mc, Base.metadata)
if not diffs:
return []
issues = []
for diff in diffs:
if diff[0] == "add_table":
issues.append(f"Missing table: {diff[1].name}")
elif diff[0] == "remove_table":
issues.append(f"Extra table: {diff[1].name}")
elif diff[0] in ("add_column", "remove_column"):
issues.append(f"{diff[0]}: {diff[2]}.{diff[3].name}")
elif diff[0] == "modify_type":
issues.append(f"Type changed: {diff[2]}.{diff[3]}: {diff[5]} -> {diff[6]}")
return issues
# GitHub Actions pre-deploy check
if __name__ == "__main__":
issues = check_schema_drift(DATABASE_URL)
if issues:
print("Schema drift detected:")
for issue in issues:
print(f" - {issue}")
sys.exit(1)
print("Schema is up to date")
🔍 Interviewer Notes: Tests CI/CD pipeline knowledge for safe database deployments.
❌ Common Mistakes:
- Not running schema checks in CI (discovering drift only at deployment time)
- Not running migrations in a pre-production environment first
- Ignoring autogenerate diffs without reviewing them
Q259: How do you implement a data archival strategy with SQLAlchemy?
Technical Answer:
Move old records from active tables to archive tables using the same schema. Use PostgreSQL table inheritance or a separate _archive table. Automate with a scheduled job that moves records older than a threshold.
Code Example:
from sqlalchemy import text
from datetime import datetime, timedelta
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column()
status: Mapped[str] = mapped_column(String(20))
created_at: Mapped[datetime] = mapped_column()
total: Mapped[float] = mapped_column()
class OrderArchive(Base):
__tablename__ = "orders_archive"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column()
status: Mapped[str] = mapped_column(String(20))
created_at: Mapped[datetime] = mapped_column()
total: Mapped[float] = mapped_column()
archived_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def archive_old_orders(session: Session, older_than_days: int = 365, batch_size: int = 1000):
cutoff = datetime.utcnow() - timedelta(days=older_than_days)
total_archived = 0
while True:
# Use RETURNING to atomically move records
result = session.execute(
text("""
WITH moved AS (
DELETE FROM orders
WHERE created_at < :cutoff AND status IN ('completed', 'cancelled')
LIMIT :batch
RETURNING *
)
INSERT INTO orders_archive
SELECT *, now() as archived_at FROM moved
RETURNING id
"""),
{"cutoff": cutoff, "batch": batch_size}
).fetchall()
session.commit()
archived_count = len(result)
total_archived += archived_count
if archived_count < batch_size:
break
return total_archived
# Scheduled archival (run via cron or Celery beat)
# Also update any queries that need to look in both tables:
def get_order(session, order_id: int):
order = session.get(Order, order_id)
if not order:
order = session.get(OrderArchive, order_id)
return order
🔍 Interviewer Notes: Tests knowledge of data lifecycle management for large-scale production systems.
❌ Common Mistakes:
- Deleting records instead of archiving (no audit trail)
- Not updating application queries to check archive table
- Moving records individually instead of in batches (too slow)
Q260: How do you implement connection pooling for multi-database setups?
Technical Answer: Create separate engine instances for primary and replica databases. Route read queries to replicas, write queries to primary. Use a custom session factory or middleware to select the appropriate engine per request.
Code Example:
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from contextvars import ContextVar
# Multiple engines
primary_engine = create_engine("postgresql://user:pass@primary:5432/db", pool_size=10)
replica_engine = create_engine("postgresql://user:pass@replica:5432/db", pool_size=20)
PrimarySession = sessionmaker(bind=primary_engine)
ReplicaSession = sessionmaker(bind=replica_engine)
use_replica: ContextVar[bool] = ContextVar("use_replica", default=False)
class RoutingSession(Session):
"""Session that routes to primary or replica based on context."""
def get_bind(self, mapper=None, clause=None, **kwargs):
# Always write to primary
if self._flushing:
return primary_engine
# Check if we're in a write transaction
if self.in_transaction() and not use_replica.get():
return primary_engine
# Route reads to replica
if use_replica.get():
return replica_engine
return primary_engine
RoutingSessionFactory = sessionmaker(class_=RoutingSession)
# Context manager for read-heavy operations
from contextlib import contextmanager
@contextmanager
def replica_context():
token = use_replica.set(True)
try:
yield
finally:
use_replica.reset(token)
# Usage
with Session() as session:
# Write
user = User(name="Alice")
session.add(user)
session.commit() # Goes to primary
with Session() as session:
with replica_context():
users = session.scalars(select(User)).all() # Goes to replica
🔍 Interviewer Notes: Tests production architecture knowledge for read-scaling with replicas.
❌ Common Mistakes:
- Reading from replica immediately after writing (replication lag)
- Not falling back to primary when replica is unavailable
- Using replica for queries inside a write transaction (inconsistency)
Q261: How do you implement a schema-per-tenant multi-tenancy pattern?
Technical Answer:
Schema-per-tenant uses a separate PostgreSQL schema per tenant (tenant_1.users, tenant_2.users). Set search_path per connection to route queries to the right tenant schema automatically.
Code Example:
from sqlalchemy import event, text
from contextvars import ContextVar
tenant_schema: ContextVar[str] = ContextVar("tenant_schema", default="public")
@event.listens_for(engine, "checkout")
def set_search_path(dbapi_conn, connection_record, connection_proxy):
schema = tenant_schema.get()
cursor = dbapi_conn.cursor()
cursor.execute(f"SET search_path TO {schema}, public")
cursor.close()
# Alembic: run migrations for all tenants
def upgrade_all_tenants(alembic_cfg, tenant_schemas: list[str]):
for schema in tenant_schemas:
alembic_cfg.set_main_option("version_schema", schema)
alembic_cfg.set_main_option("target_metadata_schema", schema)
# Run migrations with schema set
command.upgrade(alembic_cfg, "head")
# Create tenant schema
def create_tenant(session, tenant_name: str):
schema = f"tenant_{tenant_name}"
session.execute(text(f"CREATE SCHEMA IF NOT EXISTS {schema}"))
session.commit()
# Create all tables in new schema
token = tenant_schema.set(schema)
try:
with engine.connect() as conn:
Base.metadata.create_all(conn)
finally:
tenant_schema.reset(token)
# FastAPI middleware
class TenantMiddleware:
async def __call__(self, request, call_next):
subdomain = request.headers.get("X-Tenant") or "public"
schema = f"tenant_{subdomain}"
token = tenant_schema.set(schema)
try:
return await call_next(request)
finally:
tenant_schema.reset(token)
🔍 Interviewer Notes: Tests multi-tenancy architecture and PostgreSQL schema isolation.
❌ Common Mistakes:
- SQL injection via
search_pathif tenant name comes from user input (validate strictly) - Not managing Alembic migrations per schema
- Not testing schema isolation between tenants
Q262: How do you implement streaming large result sets with async SQLAlchemy?
Technical Answer:
Use AsyncSession.stream() and AsyncSession.stream_scalars() for server-side cursor-based streaming. This avoids loading all rows into memory. Combine with yield_per() for batch control.
Code Example:
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
async def stream_all_users(session: AsyncSession):
"""Stream users without loading all into memory."""
stmt = select(User).order_by(User.id).execution_options(yield_per=500)
# stream_scalars returns an AsyncScalarResult
async with await session.stream_scalars(stmt) as stream:
async for user in stream:
yield user
# FastAPI streaming response
from fastapi.responses import StreamingResponse
import json
async def export_users_json(session: AsyncSession) -> StreamingResponse:
async def generate():
yield "["
first = True
async for user in stream_all_users(session):
if not first:
yield ","
yield json.dumps({"id": user.id, "name": user.name, "email": user.email})
first = False
yield "]"
return StreamingResponse(generate(), media_type="application/json")
# Streaming with partitions (process in batches)
async def process_all_orders(session: AsyncSession, processor):
stmt = select(Order).where(Order.status == "pending").execution_options(yield_per=100)
async with await session.stream(stmt) as stream:
async for partition in stream.partitions(100):
# Each partition is a list of rows
await processor.process_batch([row[0] for row in partition])
🔍 Interviewer Notes: Tests async SQLAlchemy streaming for memory-efficient large-dataset processing.
❌ Common Mistakes:
- Using
scalars().all()for large result sets (loads everything into memory) - Not using
execution_options(yield_per=...)(server-side cursor not activated) - Holding the async stream open while doing slow I/O per row
Q263: How do you handle database migrations with zero downtime deployments?
Technical Answer: Zero-downtime migrations require backward-compatible changes: expand (add nullable columns/new tables), then contract (remove old columns) in a subsequent deployment. Never drop columns or rename them in the same deployment as the code change.
Code Example:
# PHASE 1: Expansion migration (deploy with old code still running)
def upgrade_phase1():
# Add new nullable column (old code ignores it)
op.add_column("users", sa.Column("full_name", sa.String(200), nullable=True))
# Copy data in batches
op.execute("""
UPDATE users SET full_name = first_name || ' ' || last_name
WHERE full_name IS NULL
""")
# PHASE 2: Code update (uses new column, falls back to old)
# Deploy new code that reads full_name if available, else first_name + last_name
# PHASE 3: Contraction migration (after old code is fully retired)
def upgrade_phase3():
# Make column NOT NULL now that all rows have data
op.alter_column("users", "full_name", nullable=False)
# Drop old columns (safe now)
op.drop_column("users", "first_name")
op.drop_column("users", "last_name")
# Adding an index: use CONCURRENTLY (non-blocking)
def upgrade():
# Regular CREATE INDEX locks the table
# op.create_index("ix_users_email", "users", ["email"]) # AVOID in prod
# CONCURRENTLY doesn't lock writes
op.execute(
"CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_users_email ON users (email)"
)
# But: can't run inside a transaction — use transaction_per_migration=False
🔍 Interviewer Notes: Tests production deployment knowledge for live systems.
❌ Common Mistakes:
- Renaming a column atomically with code change (causes errors during deployment)
- Creating indexes without
CONCURRENTLYon large tables (locks all writes) - Not using feature flags to gradually migrate to new schema
Q264: How do you implement custom comparison operators in SQLAlchemy?
Technical Answer:
Use TypeDecorator.coerce_compared_value() or custom_op() to define custom SQL operators. PostgreSQL supports many operators via extensions (PostGIS, ltree, etc.) accessible via .op().
Code Example:
from sqlalchemy import TypeDecorator, String, func
from sqlalchemy.dialects.postgresql import ARRAY
# Custom similarity operator using pg_trgm
class SimilarityType(TypeDecorator):
impl = String
cache_ok = True
class comparator_factory(String.Comparator):
def similar_to(self, other, threshold=0.3):
return func.similarity(self.expr, other) >= threshold
def similarity_score(self, other):
return func.similarity(self.expr, other)
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(primary_key=True)
name = mapped_column(SimilarityType)
# Usage with custom operator
products = session.scalars(
select(Product)
.where(Product.name.similar_to("laptoppp", threshold=0.4)) # typo-tolerant
.order_by(Product.name.similarity_score("laptoppp").desc())
).all()
# Raw custom operators using .op()
# Inet operations
from sqlalchemy.dialects.postgresql import INET, CIDR
class NetworkRule(Base):
__tablename__ = "network_rules"
id: Mapped[int] = mapped_column(primary_key=True)
network = Column(CIDR)
# Check if IP is in network: '192.168.1.5' << '192.168.1.0/24'
from sqlalchemy import cast, literal
result = session.scalars(
select(NetworkRule).where(
cast(literal("192.168.1.5"), INET).op("<<")(NetworkRule.network)
)
).all()
🔍 Interviewer Notes: Tests advanced SQLAlchemy type system and PostgreSQL operator knowledge.
❌ Common Mistakes:
- Not setting
cache_ok = Trueon custom types (prevents query caching) - Using Python operators for PostgreSQL-specific operations
- Not creating a trigram index for similarity operations (full scan without it)
Q265: How do you implement database partitioning with SQLAlchemy?
Technical Answer:
PostgreSQL declarative partitioning (RANGE, LIST, HASH) requires DDL that SQLAlchemy doesn't auto-generate. Create partitioned tables via op.execute() in Alembic and attach partitions. SQLAlchemy queries work transparently via the parent table.
Code Example:
# Alembic migration: range partitioning by created_at
def upgrade():
# Create parent partitioned table
op.execute("""
CREATE TABLE events (
id BIGSERIAL,
created_at TIMESTAMPTZ NOT NULL,
type VARCHAR(50),
payload JSONB,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at)
""")
# Create monthly partitions
op.execute("""
CREATE TABLE events_2026_01 PARTITION OF events
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01')
""")
op.execute("""
CREATE TABLE events_2026_02 PARTITION OF events
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01')
""")
# Default partition for anything out of range
op.execute("""
CREATE TABLE events_default PARTITION OF events DEFAULT
""")
# Create future partitions automatically (via cron)
def create_next_month_partition(session, year: int, month: int):
from calendar import monthrange
import datetime
start = datetime.date(year, month, 1)
_, days = monthrange(year, month)
end = start + datetime.timedelta(days=days)
partition_name = f"events_{year}_{month:02d}"
session.execute(text(f"""
CREATE TABLE IF NOT EXISTS {partition_name} PARTITION OF events
FOR VALUES FROM ('{start}') TO ('{end}')
"""))
session.commit()
# SQLAlchemy model works on parent table
class Event(Base):
__tablename__ = "events"
id: Mapped[int] = mapped_column(primary_key=True)
created_at: Mapped[datetime] = mapped_column()
type: Mapped[str] = mapped_column(String(50))
payload: Mapped[dict] = mapped_column(JSON)
🔍 Interviewer Notes: Tests knowledge of PostgreSQL partitioning for time-series and large-scale data.
❌ Common Mistakes:
- Not including partition key in the PRIMARY KEY (PostgreSQL requirement)
- Not creating a default partition (rows outside ranges cause errors)
- Not automating partition creation before the time period arrives
Q266: How do you implement audit logging that captures the requesting user?
Technical Answer:
Use a ContextVar to store the current user ID, then capture it in before_insert/before_update events. Store in an audit_logs table with user ID, action, entity, and timestamp.
Code Example:
from contextvars import ContextVar
from sqlalchemy import event, JSON, String, Integer
current_user_id: ContextVar[Optional[int]] = ContextVar("current_user_id", default=None)
class AuditLog(Base):
__tablename__ = "audit_logs"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[Optional[int]] = mapped_column()
action: Mapped[str] = mapped_column(String(20))
entity_type: Mapped[str] = mapped_column(String(100))
entity_id: Mapped[Optional[int]] = mapped_column()
changes: Mapped[Optional[dict]] = mapped_column(JSON)
ip_address: Mapped[Optional[str]] = mapped_column(String(45))
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def audit(session: Session, action: str, entity_type: str,
entity_id: Optional[int], changes: Optional[dict] = None):
session.add(AuditLog(
user_id=current_user_id.get(),
action=action,
entity_type=entity_type,
entity_id=entity_id,
changes=changes,
))
# FastAPI dependency: set user in context var
async def set_audit_user(request: Request, user: User = Depends(get_current_user)):
token = current_user_id.set(user.id)
try:
yield
finally:
current_user_id.reset(token)
# Auto-audit on model changes via events
@event.listens_for(User, "after_update")
def user_updated(mapper, connection, target):
from sqlalchemy.orm import attributes
changes = {}
for attr in mapper.attrs:
hist = attributes.get_history(target, attr.key)
if hist.has_changes() and attr.key not in ("updated_at",):
changes[attr.key] = {
"old": hist.deleted[0] if hist.deleted else None,
"new": getattr(target, attr.key)
}
if changes:
connection.execute(
AuditLog.__table__.insert().values(
user_id=current_user_id.get(),
action="UPDATE",
entity_type="User",
entity_id=target.id,
changes=changes,
)
)
🔍 Interviewer Notes: Tests audit trail implementation with user context propagation.
❌ Common Mistakes:
- Not capturing the requesting user (audit log without actor is incomplete)
- Using a separate audit session (misses rollbacks)
- Logging all attribute changes including unimportant ones (noisy audit logs)
Q267: How do you implement batch operations efficiently in async SQLAlchemy?
Technical Answer:
For bulk inserts in async SQLAlchemy, use session.execute(insert(Model).values([...])) with a list of dicts. For bulk updates, use the update() construct. Avoid session.add() in a loop — it's N separate INSERTs.
Code Example:
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import insert, update, delete
async def bulk_insert_users(session: AsyncSession, users_data: list[dict]):
"""Single INSERT with multiple rows."""
await session.execute(
insert(User),
users_data # List of dicts: [{"name": "Alice", "email": "..."}, ...]
)
await session.commit()
async def bulk_update_status(session: AsyncSession, user_ids: list[int], status: str):
"""Single UPDATE for multiple rows."""
await session.execute(
update(User)
.where(User.id.in_(user_ids))
.values(is_active=(status == "active"))
)
await session.commit()
# Chunked bulk insert for very large datasets
async def bulk_insert_chunked(session: AsyncSession, data: list[dict], chunk_size: int = 1000):
for i in range(0, len(data), chunk_size):
chunk = data[i:i + chunk_size]
await session.execute(insert(User), chunk)
await session.commit()
# Insert with RETURNING (PostgreSQL)
from sqlalchemy.dialects.postgresql import insert as pg_insert
async def bulk_upsert_and_return(session: AsyncSession, data: list[dict]) -> list[int]:
stmt = (
pg_insert(User)
.values(data)
.on_conflict_do_update(
index_elements=["email"],
set_={"name": pg_insert(User).excluded.name}
)
.returning(User.id)
)
result = await session.execute(stmt)
await session.commit()
return result.scalars().all()
🔍 Interviewer Notes: Tests bulk operation patterns for high-throughput async systems.
❌ Common Mistakes:
- Using
session.add()in a loop (N+1 INSERT problem) - Not chunking very large datasets (memory exhaustion)
- Not using
RETURNINGwhen the caller needs the generated IDs
Q268: How do you implement a deduplication pattern with SQLAlchemy?
Technical Answer:
Use a idempotency_key unique column to prevent duplicate processing. On duplicate insert, return the existing record. Combine with ON CONFLICT DO NOTHING ... RETURNING for atomic deduplication.
Code Example:
from sqlalchemy.dialects.postgresql import insert as pg_insert
import hashlib
import json
class Payment(Base):
__tablename__ = "payments"
id: Mapped[int] = mapped_column(primary_key=True)
idempotency_key: Mapped[str] = mapped_column(String(64), unique=True)
amount: Mapped[float] = mapped_column()
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
status: Mapped[str] = mapped_column(String(20), default="pending")
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def generate_idempotency_key(user_id: int, amount: float, reference: str) -> str:
data = {"user_id": user_id, "amount": amount, "reference": reference}
return hashlib.sha256(json.dumps(data, sort_keys=True).encode()).hexdigest()
def create_or_get_payment(
session: Session, user_id: int, amount: float, reference: str
) -> tuple[Payment, bool]:
"""Returns (payment, was_created)."""
key = generate_idempotency_key(user_id, amount, reference)
# Try insert, get existing on conflict
stmt = (
pg_insert(Payment)
.values(idempotency_key=key, amount=amount, user_id=user_id)
.on_conflict_do_nothing(index_elements=["idempotency_key"])
.returning(Payment.id, Payment.status)
)
result = session.execute(stmt).fetchone()
if result:
session.commit()
return session.get(Payment, result[0]), True
# Conflict: return existing
existing = session.scalars(
select(Payment).where(Payment.idempotency_key == key)
).one()
return existing, False
🔍 Interviewer Notes: Tests idempotency patterns for payment and financial systems.
❌ Common Mistakes:
- Not using a unique constraint on the idempotency key
- Generating keys non-deterministically (UUID instead of hash)
- Not returning the existing record on duplicate (breaks caller retry logic)
Q269: How do you implement a connection retry with circuit breaker for SQLAlchemy?
Technical Answer: A circuit breaker tracks consecutive failures. After a threshold, it opens (fails fast without attempting DB connection). After a timeout, it half-opens (allows one probe request). Implement with a state machine stored in a shared counter.
Code Example:
import time
from enum import Enum
from threading import Lock
class CircuitState(Enum):
CLOSED = "closed" # Normal operation
OPEN = "open" # Failing fast
HALF_OPEN = "half_open" # Testing if DB recovered
class DBCircuitBreaker:
def __init__(self, failure_threshold=5, timeout=60):
self.failure_threshold = failure_threshold
self.timeout = timeout
self._state = CircuitState.CLOSED
self._failures = 0
self._opened_at = 0
self._lock = Lock()
def call(self, func, *args, **kwargs):
with self._lock:
if self._state == CircuitState.OPEN:
if time.time() - self._opened_at > self.timeout:
self._state = CircuitState.HALF_OPEN
else:
raise Exception("Circuit breaker OPEN — DB unavailable")
try:
result = func(*args, **kwargs)
self._on_success()
return result
except Exception as e:
self._on_failure()
raise
def _on_success(self):
with self._lock:
self._failures = 0
self._state = CircuitState.CLOSED
def _on_failure(self):
with self._lock:
self._failures += 1
if self._failures >= self.failure_threshold:
self._state = CircuitState.OPEN
self._opened_at = time.time()
db_breaker = DBCircuitBreaker(failure_threshold=3, timeout=30)
def get_user_safely(session_factory, user_id: int):
def _query():
with session_factory() as session:
return session.get(User, user_id)
return db_breaker.call(_query)
🔍 Interviewer Notes: Tests resilience patterns for production systems under partial failure.
❌ Common Mistakes:
- Not distinguishing between retriable errors (connection lost) and permanent errors (bad SQL)
- Not monitoring circuit breaker state (alert when OPEN)
- Opening the circuit too aggressively (noisy network causes false trips)
Q270: How do you implement complete observability for SQLAlchemy in production?
Technical Answer: Combine three pillars: (1) metrics via Prometheus (query count, duration, pool stats), (2) tracing via OpenTelemetry SQLAlchemy instrumentation, (3) structured logging of slow queries with context.
Code Example:
from opentelemetry import trace
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from prometheus_client import Histogram, Counter, Gauge
import structlog
import time
# OpenTelemetry: auto-traces every query as a span
SQLAlchemyInstrumentor().instrument(engine=engine)
# Prometheus metrics
QUERY_DURATION = Histogram(
"db_query_duration_seconds",
"Database query duration",
buckets=[0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0],
labelnames=["operation", "table"],
)
QUERY_COUNT = Counter("db_query_total", "Total queries", labelnames=["operation"])
POOL_GAUGE = Gauge("db_pool_checked_out", "Connections in use")
log = structlog.get_logger()
# SQLAlchemy event listeners for metrics + logging
@event.listens_for(engine, "before_cursor_execute")
def before_execute(conn, cursor, stmt, params, context, executemany):
conn.info["_query_start"] = time.monotonic()
conn.info["_query_stmt"] = stmt
@event.listens_for(engine, "after_cursor_execute")
def after_execute(conn, cursor, stmt, params, context, executemany):
elapsed = time.monotonic() - conn.info.pop("_query_start", time.monotonic())
# Extract operation type
op = stmt.strip().split()[0].upper()
QUERY_COUNT.labels(operation=op).inc()
QUERY_DURATION.labels(operation=op, table="unknown").observe(elapsed)
if elapsed > 0.5:
log.warning(
"slow_query",
duration_ms=round(elapsed * 1000, 1),
operation=op,
query=stmt[:200],
)
@event.listens_for(engine, "checkout")
def on_checkout(dbapi_conn, record, proxy):
POOL_GAUGE.inc()
@event.listens_for(engine, "checkin")
def on_checkin(dbapi_conn, record):
POOL_GAUGE.dec()
🔍 Interviewer Notes: Tests production observability knowledge covering metrics, tracing, and logging.
❌ Common Mistakes:
- Only logging errors (missing slow queries and pool exhaustion)
- Not correlating DB traces with request traces (need same trace ID)
- Logging full query with parameters (may contain PII/credentials)
Q271: How do you implement a complete testing strategy for SQLAlchemy repositories?
Technical Answer: Layer tests: unit tests with mocked sessions for business logic, integration tests with real PostgreSQL for SQL correctness, and contract tests for API boundaries. Mock only at the repository interface boundary, never inside repositories.
Code Example:
import pytest
from unittest.mock import MagicMock, patch
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
# Layer 1: Unit test — mock the repository interface
def test_user_service_creates_audit_log():
mock_repo = MagicMock()
mock_repo.find_by_email.return_value = None
service = UserService(user_repo=mock_repo)
service.register("alice@example.com", "Alice")
mock_repo.save.assert_called_once()
# Verify the saved user has correct fields
saved_user = mock_repo.save.call_args[0][0]
assert saved_user.email == "alice@example.com"
# Layer 2: Integration test — real PostgreSQL
@pytest.fixture(scope="module")
def test_engine():
e = create_engine("postgresql://test:test@localhost/test_db")
Base.metadata.create_all(e)
yield e
Base.metadata.drop_all(e)
@pytest.fixture
def db(test_engine):
conn = test_engine.connect()
tx = conn.begin()
session = Session(bind=conn)
session.begin_nested()
yield session
session.close()
tx.rollback()
conn.close()
def test_repository_finds_by_email(db):
repo = SQLAlchemyUserRepository(db)
user = User(name="Alice", email="alice@test.com")
repo.save(user)
found = repo.find_by_email("alice@test.com")
assert found is not None
assert found.name == "Alice"
def test_repository_returns_none_for_unknown_email(db):
repo = SQLAlchemyUserRepository(db)
found = repo.find_by_email("unknown@test.com")
assert found is None
# Layer 3: Verify schema constraints
def test_duplicate_email_raises(db):
repo = SQLAlchemyUserRepository(db)
repo.save(User(name="Alice", email="dup@test.com"))
db.flush()
with pytest.raises(Exception): # IntegrityError
repo.save(User(name="Bob", email="dup@test.com"))
db.flush()
🔍 Interviewer Notes: Tests understanding of testing pyramid for database-heavy code.
❌ Common Mistakes:
- Mocking the database inside the repository (integration test value lost)
- Only writing unit tests for SQL-heavy code (mocks mask real query behavior)
- Not testing constraint violations (assumes app-level validation is sufficient)
Q272: How do you implement a query result transformation layer?
Technical Answer:
Transform ORM results to DTOs/response schemas using Pydantic model_validate(obj) or a custom mapper. Keep transformations outside repositories (single responsibility). Use __init_subclass__ for auto-registration of DTO converters.
Code Example:
from pydantic import BaseModel, ConfigDict
from typing import Optional, List
# DTOs (Data Transfer Objects)
class UserDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
name: str
email: str
post_count: Optional[int] = None # Computed field
class PostSummaryDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
title: str
class UserWithPostsDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
name: str
posts: List[PostSummaryDTO]
# Service layer: queries + transforms
def get_user_with_post_count(session: Session, user_id: int) -> Optional[UserDTO]:
result = session.execute(
select(
User,
func.count(Post.id).label("post_count")
)
.outerjoin(Post)
.where(User.id == user_id)
.group_by(User.id)
).first()
if not result:
return None
user, post_count = result
# Manual DTO construction for computed fields
return UserDTO(
id=user.id,
name=user.name,
email=user.email,
post_count=post_count
)
def get_users_with_posts(session: Session, limit: int = 20) -> List[UserWithPostsDTO]:
users = session.scalars(
select(User)
.options(selectinload(User.posts))
.limit(limit)
).all()
# Pydantic handles ORM -> DTO conversion
return [UserWithPostsDTO.model_validate(u) for u in users]
🔍 Interviewer Notes: Tests separation of concerns between persistence and presentation layers.
❌ Common Mistakes:
- Returning ORM objects directly from API endpoints (leaks schema, causes lazy load)
- Putting transformation logic inside repositories
- Not using
from_attributes=True(Pydantic won't read from ORM attributes)
Q273: How does SQLAlchemy handle None vs null() in queries?
Technical Answer:
In SQLAlchemy 2.0, comparing with == None generates IS NULL, not = NULL. This is intentional SQL-correct behavior. Use .is_(None) for explicit NULL checks. null() is a SQL NULL literal for INSERT/UPDATE values.
Code Example:
from sqlalchemy import null, select, func
# == None generates IS NULL (correct)
stmt = select(User).where(User.deleted_at == None)
# SQL: WHERE deleted_at IS NULL ✓
# != None generates IS NOT NULL (correct)
stmt = select(User).where(User.deleted_at != None)
# SQL: WHERE deleted_at IS NOT NULL ✓
# Explicit: use .is_(None) and .is_not(None) for clarity
stmt = select(User).where(User.deleted_at.is_(None))
stmt = select(User).where(User.deleted_at.is_not(None))
# null() literal: for INSERT/UPDATE with NULL value
session.execute(
User.__table__.update()
.where(User.id == 1)
.values(deleted_at=null()) # Explicitly set NULL
)
# NULL in aggregates
stmt = select(
func.count(User.id), # Counts all rows
func.count(User.deleted_at), # Counts non-NULL deleted_at
func.sum(User.score), # Ignores NULLs
func.coalesce(User.score, 0), # Replace NULL with 0
)
# NULLS FIRST / NULLS LAST in ORDER BY
stmt = select(User).order_by(User.last_login.desc().nulls_last())
🔍 Interviewer Notes: Tests understanding of NULL semantics in SQL and SQLAlchemy.
❌ Common Mistakes:
- Using
WHERE column = NULL(always False in SQL) - Not using
NULLS LASTfor optional sort fields (NULLs sort first by default in PostgreSQL DESC) - Not handling NULL in aggregates (COUNT(*) vs COUNT(col) difference)
Q274: How do you implement a connection string vault with dynamic credentials?
Technical Answer: Use AWS IAM authentication for RDS or Vault's database secrets engine. Credentials are short-lived (15min for RDS IAM). Implement token refresh by catching authentication errors and regenerating the auth token.
Code Example:
import boto3
from sqlalchemy import create_engine, event
from sqlalchemy.pool import NullPool
import time
class RDSIAMEngine:
"""SQLAlchemy engine with RDS IAM authentication."""
def __init__(self, host: str, port: int, user: str, db: str, region: str):
self.host = host
self.port = port
self.user = user
self.db = db
self.region = region
self._token = None
self._token_expiry = 0
def _get_token(self) -> str:
if time.time() > self._token_expiry - 60: # Refresh 1min before expiry
client = boto3.client("rds", region_name=self.region)
self._token = client.generate_db_auth_token(
DBHostname=self.host,
Port=self.port,
DBUsername=self.user
)
self._token_expiry = time.time() + 900 # 15 min
return self._token
def get_engine(self):
def get_url():
token = self._get_token()
return (f"postgresql+psycopg2://{self.user}:{token}"
f"@{self.host}:{self.port}/{self.db}"
f"?sslmode=require")
engine = create_engine(
get_url(),
pool=NullPool, # Don't pool — tokens expire
connect_args={"sslmode": "require"},
)
@event.listens_for(engine, "do_connect")
def provide_token(dialect, conn_rec, cargs, cparams):
# Refresh token on each connection
cparams["password"] = self._get_token()
return engine
# Usage
rds_engine = RDSIAMEngine(
host="mydb.cluster.us-east-1.rds.amazonaws.com",
port=5432,
user="app_user",
db="mydb",
region="us-east-1"
).get_engine()
🔍 Interviewer Notes: Tests production security knowledge for cloud database authentication.
❌ Common Mistakes:
- Caching IAM tokens indefinitely (they expire in 15 minutes)
- Using long-lived passwords when IAM auth is available
- Not using SSL with RDS IAM auth (tokens can be sniffed without it)
Q275: How do you implement a query plan cache with SQLAlchemy?
Technical Answer:
PostgreSQL caches prepared statement plans per connection. SQLAlchemy's compiled_cache caches compiled SQL strings per engine. For ORM queries, enable cache_ok=True on custom types and avoid non-cacheable constructs.
Code Example:
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool
# SQLAlchemy's compiled cache (enabled by default in 1.4+)
engine = create_engine(
DATABASE_URL,
# Control compiled cache size (default: 500 entries per engine)
query_cache_size=1000,
)
# Cache-friendly: static query structure
def get_user_by_id(session: Session, user_id: int) -> Optional[User]:
# This query is compiled once and cached
return session.get(User, user_id)
# Cache-unfriendly: dynamic IN clause (different number of params each time)
def get_users_by_ids_bad(session: Session, ids: list[int]) -> list[User]:
# Different SQL for each len(ids) — generates cache thrash
return session.scalars(select(User).where(User.id.in_(ids))).all()
# Cache-friendly: use expand_in for IN clauses
from sqlalchemy import bindparam
def get_users_by_ids_good(session: Session, ids: list[int]) -> list[User]:
# Use expanding bindparam — cached with IN (?,?,?) template
return session.scalars(
select(User).where(User.id.in_(bindparam("ids", expanding=True))),
{"ids": ids}
).all()
# Check cache performance
from sqlalchemy import event
cache_hits = cache_misses = 0
@event.listens_for(engine, "before_cursor_execute")
def track_cache(conn, cursor, stmt, params, context, executemany):
global cache_hits, cache_misses
if context.compiled_cache is not None:
if stmt in context.compiled_cache:
cache_hits += 1
else:
cache_misses += 1
🔍 Interviewer Notes: Tests understanding of query compilation caching for performance.
❌ Common Mistakes:
- Using dynamic
INclauses that prevent caching - Setting
query_cache_size=0(disables caching entirely) - Not marking custom types with
cache_ok=True(causes cache bypass)
Q276: How do you implement a multi-model bulk operation with rollback?
Technical Answer:
Wrap multi-model bulk operations in a single transaction using context management. Use session.bulk_insert_mappings() for maximum performance, but note it bypasses ORM events. Use session.execute(insert()) to keep events.
Code Example:
from sqlalchemy import insert
from sqlalchemy.exc import IntegrityError
def import_organization(session: Session, org_data: dict) -> dict:
"""Import org with users, roles, and permissions atomically."""
try:
# Insert organization
org_stmt = insert(Organization).values(
name=org_data["name"], slug=org_data["slug"]
).returning(Organization.id)
org_id = session.execute(org_stmt).scalar_one()
# Bulk insert users
users_data = [
{"name": u["name"], "email": u["email"], "org_id": org_id}
for u in org_data.get("users", [])
]
if users_data:
user_ids = session.execute(
insert(User).values(users_data).returning(User.id, User.email)
).fetchall()
# Bulk insert roles
roles_data = [
{"name": r["name"], "org_id": org_id}
for r in org_data.get("roles", [])
]
if roles_data:
session.execute(insert(Role).values(roles_data))
session.commit()
return {"org_id": org_id, "user_count": len(users_data)}
except IntegrityError as e:
session.rollback()
raise ValueError(f"Import failed — duplicate data: {e.orig}")
except Exception:
session.rollback()
raise
# For performance-critical imports: use bulk_insert_mappings
def fast_import(session: Session, users: list[dict]):
"""Bypasses ORM — no events, no validation, maximum speed."""
session.bulk_insert_mappings(User, users)
session.commit()
🔍 Interviewer Notes: Tests bulk operation patterns and transaction management knowledge.
❌ Common Mistakes:
- Not wrapping multi-step operations in a transaction (partial failures leave inconsistent state)
- Using
bulk_insert_mappingswhen ORM events (audit logging, validation) are needed - Not handling
IntegrityErrorin import operations (duplicate data breaks the batch)
Q277: How do you implement a database-backed feature flag system with SQLAlchemy?
Technical Answer:
Store feature flags in a feature_flags table with rules (user percentage rollout, user list, etc.). Cache the flags in memory with a short TTL to avoid DB queries on every request.
Code Example:
import time
from functools import lru_cache
class FeatureFlag(Base):
__tablename__ = "feature_flags"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100), unique=True)
enabled: Mapped[bool] = mapped_column(default=False)
rollout_percentage: Mapped[int] = mapped_column(default=100)
allowed_user_ids: Mapped[Optional[list]] = mapped_column(JSON, default=None)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
class FeatureFlagService:
def __init__(self, session_factory, cache_ttl: int = 60):
self._factory = session_factory
self._cache: dict = {}
self._cache_ttl = cache_ttl
def _load_flags(self) -> dict:
now = time.time()
if self._cache.get("_loaded_at", 0) + self._cache_ttl < now:
with self._factory() as session:
flags = session.scalars(select(FeatureFlag)).all()
self._cache = {f.name: f for f in flags}
self._cache["_loaded_at"] = now
return self._cache
def is_enabled(self, flag_name: str, user_id: Optional[int] = None) -> bool:
flags = self._load_flags()
flag = flags.get(flag_name)
if not flag or not flag.enabled:
return False
# Specific user allowlist
if flag.allowed_user_ids and user_id in flag.allowed_user_ids:
return True
# Percentage rollout based on user_id hash
if user_id and flag.rollout_percentage < 100:
return (user_id % 100) < flag.rollout_percentage
return True
# FastAPI dependency
feature_flags = FeatureFlagService(session_factory)
@router.get("/products/{product_id}")
def get_product(product_id: int, user: User = Depends(get_current_user)):
product = get_product_from_db(product_id)
if feature_flags.is_enabled("new_pricing_engine", user.id):
product.price = new_pricing_engine.calculate(product)
return product
🔍 Interviewer Notes: Tests feature flag implementation and cache invalidation strategies.
❌ Common Mistakes:
- Querying the DB on every request for flag state (high latency impact)
- Not providing a way to invalidate the cache immediately for urgent flag changes
- Using percentage rollout without consistent hashing (user sees flag flicker)
Q278: How do you implement connection string parsing and validation with SQLAlchemy?
Technical Answer:
Use sqlalchemy.engine.make_url() and URL.create() to safely construct and parse connection strings. Always validate before creating the engine to provide clear error messages.
Code Example:
from sqlalchemy.engine import make_url, URL
from sqlalchemy.exc import ArgumentError
import os
def parse_database_url(url_string: str) -> URL:
"""Parse and validate a database URL."""
try:
url = make_url(url_string)
except ArgumentError as e:
raise ValueError(f"Invalid database URL format: {e}")
if url.drivername not in ("postgresql", "postgresql+psycopg2", "postgresql+asyncpg"):
raise ValueError(f"Unsupported database driver: {url.drivername}")
if not url.database:
raise ValueError("Database name is required in connection URL")
return url
def create_engine_from_env() -> Engine:
"""Create engine from environment variables with validation."""
# Option 1: Full URL
db_url = os.getenv("DATABASE_URL")
if db_url:
url = parse_database_url(db_url)
else:
# Option 2: Individual components
url = URL.create(
drivername="postgresql+psycopg2",
username=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"], # Handled safely — not logged
host=os.environ["DB_HOST"],
port=int(os.getenv("DB_PORT", "5432")),
database=os.environ["DB_NAME"],
)
return create_engine(
url,
pool_pre_ping=True,
echo=os.getenv("DB_ECHO", "false").lower() == "true",
)
# Redact password for logging
def safe_db_url(url: URL) -> str:
return url.render_as_string(hide_password=True)
engine = create_engine_from_env()
print(f"Connected to: {safe_db_url(engine.url)}")
# Output: postgresql+psycopg2://user:***@host:5432/db
🔍 Interviewer Notes: Tests secure credential handling and configuration validation.
❌ Common Mistakes:
- Logging the full connection URL with password
- Using string formatting for URLs (f-string passwords break on special chars)
- Not validating the URL format before attempting connection
Q279: How do you implement a database migration with dependent view updates?
Technical Answer:
PostgreSQL views that depend on a column being modified must be dropped and recreated. Use op.execute() in Alembic to manage view lifecycle around the column change in the correct order.
Code Example:
from alembic import op
import sqlalchemy as sa
# Alembic migration: rename column that a view depends on
def upgrade():
# Step 1: Drop dependent views
op.execute("DROP VIEW IF EXISTS active_user_stats CASCADE")
op.execute("DROP MATERIALIZED VIEW IF EXISTS monthly_revenue CASCADE")
# Step 2: Modify the column
op.alter_column("users", "first_name", new_column_name="given_name")
# Step 3: Recreate views with new column name
op.execute("""
CREATE OR REPLACE VIEW active_user_stats AS
SELECT
u.id,
u.given_name || ' ' || u.last_name AS full_name,
COUNT(o.id) AS order_count,
SUM(o.total) AS lifetime_value
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.is_active = true
GROUP BY u.id, u.given_name, u.last_name
""")
op.execute("""
CREATE MATERIALIZED VIEW monthly_revenue AS
SELECT
date_trunc('month', created_at) AS month,
SUM(total) AS revenue
FROM orders
GROUP BY 1
WITH DATA
""")
# Recreate index on materialized view
op.execute("CREATE INDEX ON monthly_revenue (month)")
def downgrade():
op.execute("DROP VIEW IF EXISTS active_user_stats CASCADE")
op.execute("DROP MATERIALIZED VIEW IF EXISTS monthly_revenue CASCADE")
op.alter_column("users", "given_name", new_column_name="first_name")
# Recreate original views...
🔍 Interviewer Notes: Tests Alembic proficiency for managing complex PostgreSQL object dependencies.
❌ Common Mistakes:
- Not using
CASCADEin DROP (leaves orphaned dependent objects) - Not recreating indexes on materialized views after DROP/CREATE
- Forgetting to recreate the view in
downgrade()
Q280: How do you implement a complete SQLAlchemy + FastAPI + Alembic production setup?
Technical Answer: A production setup requires: async engine with proper pooling, lifespan-based session management, Alembic auto-migration on startup (with advisory lock), health checks, and structured logging with query timing.
Code Example:
# database.py
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import DeclarativeBase
import os
DATABASE_URL = os.environ["DATABASE_URL"].replace("postgresql://", "postgresql+asyncpg://")
engine = create_async_engine(
DATABASE_URL,
pool_size=10,
max_overflow=20,
pool_pre_ping=True,
pool_recycle=3600,
connect_args={"statement_cache_size": 0}, # pgBouncer compat
)
AsyncSessionLocal = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
class Base(DeclarativeBase):
pass
# main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI, Depends
from alembic.config import Config
from alembic import command
@asynccontextmanager
async def lifespan(app: FastAPI):
# Run migrations on startup (with advisory lock)
alembic_cfg = Config("alembic.ini")
command.upgrade(alembic_cfg, "head")
yield
# Cleanup
await engine.dispose()
app = FastAPI(lifespan=lifespan)
async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
try:
yield session
except Exception:
await session.rollback()
raise
# Health checks
@app.get("/health/ready")
async def readiness(session: AsyncSession = Depends(get_db)):
from sqlalchemy import text
await session.execute(text("SELECT 1"))
pool = engine.pool
return {
"status": "ready",
"pool": {
"size": pool.size(),
"checked_out": pool.checkedout(),
}
}
# Example endpoint
@app.get("/users/{user_id}")
async def get_user(user_id: int, session: AsyncSession = Depends(get_db)):
from sqlalchemy import select
user = await session.scalar(select(User).where(User.id == user_id))
if not user:
raise HTTPException(status_code=404)
return UserDTO.model_validate(user)
🔍 Interviewer Notes: Tests end-to-end production setup knowledge — the most comprehensive question.
❌ Common Mistakes:
- Not using
lifespan(deprecated@app.on_eventdoesn't clean up reliably) - Using synchronous engine in async context
- Not disposing engine on shutdown (orphaned connections)
- Running migrations without advisory lock (race condition in multi-replica deploys)
Summary: SQLAlchemy Interview Topics Cheat Sheet
| Level | Key Topics |
|---|---|
| Core (Q1–Q100) | Engine, Session, ORM models, CRUD, filtering, relationships, lazy/eager loading, transactions, Alembic basics, sqlalchemy_utils, TypeDecorator, inspect |
| Intermediate (Q101–Q220) | Async SQLAlchemy, connection pooling, advanced Alembic, RLS, audit logging, bulk operations, CQRS, Unit of Work, testing patterns, PostgreSQL-specific types |
| Advanced (Q221–Q280) | Custom types, sharding, CDC outbox, saga pattern, bi-temporal data, schema-per-tenant, compiled cache, circuit breaker, observability |
Q281: How do you implement a search-as-you-type feature with SQLAlchemy?
Technical Answer:
Use PostgreSQL trigram similarity (pg_trgm) for fuzzy prefix matching. Create a GIN index with gin_trgm_ops on the search column. Combine with ts_rank for relevance scoring when results overlap full-text search.
Code Example:
from sqlalchemy import text, func, Index
from sqlalchemy.dialects.postgresql import ARRAY
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(200))
__table_args__ = (
# GIN index for trigram search (requires pg_trgm extension)
Index("ix_products_name_trgm", "name", postgresql_using="gin",
postgresql_ops={"name": "gin_trgm_ops"}),
)
# Alembic: enable pg_trgm extension
def upgrade():
op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm")
def search_products(session: Session, query: str, limit: int = 10) -> list:
if len(query) < 2:
return []
# Prefix match with LIKE (uses trigram index efficiently for 3+ chars)
# Combined with similarity score for ranking
results = session.execute(
select(
Product,
func.similarity(Product.name, query).label("score")
)
.where(
func.similarity(Product.name, query) > 0.15
)
.order_by(func.similarity(Product.name, query).desc())
.limit(limit)
).all()
return [(product, score) for product, score in results]
# For prefix search specifically (faster for autocomplete)
def autocomplete_products(session: Session, prefix: str, limit: int = 10):
return session.scalars(
select(Product)
.where(Product.name.ilike(f"{prefix}%"))
.order_by(Product.name)
.limit(limit)
).all()
🔍 Interviewer Notes: Tests knowledge of fuzzy search options in PostgreSQL vs external search systems.
❌ Common Mistakes:
- Using
ILIKE '%term%'without trigram index (full table scan) - Not setting a minimum similarity threshold (too many irrelevant results)
- Not enabling
pg_trgmextension before creating the index
Q282: How do you implement optimistic concurrency with HTTP ETag headers using SQLAlchemy?
Technical Answer:
Map the database version_id or etag column to HTTP ETag/If-Match headers in your API. On update, check If-Match against stored ETag before applying changes. Return 412 Precondition Failed on mismatch.
Code Example:
from fastapi import HTTPException, Header, Response
from sqlalchemy.exc import StaleDataError
class Resource(Base):
__tablename__ = "resources"
id: Mapped[int] = mapped_column(primary_key=True)
data: Mapped[dict] = mapped_column(JSON)
version: Mapped[int] = mapped_column(default=1)
__mapper_args__ = {"version_id_col": version}
@router.get("/resources/{resource_id}", response_model=ResourceSchema)
async def get_resource(resource_id: int, response: Response,
session: AsyncSession = Depends(get_db)):
resource = await session.get(Resource, resource_id)
if not resource:
raise HTTPException(404)
# Return ETag based on version
response.headers["ETag"] = f'"{resource.version}"'
response.headers["Cache-Control"] = "max-age=60"
return resource
@router.put("/resources/{resource_id}")
async def update_resource(
resource_id: int,
data: ResourceUpdate,
if_match: Optional[str] = Header(None),
session: AsyncSession = Depends(get_db)
):
resource = await session.get(Resource, resource_id)
if not resource:
raise HTTPException(404)
# Check ETag if provided
if if_match:
expected_version = int(if_match.strip('"'))
if resource.version != expected_version:
raise HTTPException(
status_code=412,
detail=f"Resource modified. Current version: {resource.version}"
)
resource.data = data.model_dump()
try:
await session.commit()
except StaleDataError:
raise HTTPException(409, "Concurrent modification detected")
return {"version": resource.version}
🔍 Interviewer Notes: Tests HTTP caching semantics combined with database optimistic locking.
❌ Common Mistakes:
- Not stripping quotes from ETag header value
- Not returning the new ETag after a successful update
- Using integer versions without the HTTP layer (users can't do conditional requests)
Q283: How do you implement a SQLAlchemy model with virtual/computed properties?
Technical Answer:
Use column_property() for SQL-computed values (runs in DB), hybrid_property for Python+SQL dual-mode properties, and plain Python @property for pure in-memory computed values.
Code Example:
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import column_property
from sqlalchemy import func, case, select
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
subtotal: Mapped[float] = mapped_column()
discount: Mapped[float] = mapped_column(default=0.0)
tax_rate: Mapped[float] = mapped_column(default=0.10)
# Python-only property (no SQL equivalent)
@property
def formatted_id(self) -> str:
return f"ORD-{self.id:06d}"
# Hybrid: works both in Python and SQL
@hybrid_property
def total(self) -> float:
taxable = self.subtotal - self.discount
return taxable + (taxable * self.tax_rate)
@total.expression
def total(cls):
taxable = cls.subtotal - cls.discount
return taxable + (taxable * cls.tax_rate)
# SQL-level computed: always runs aggregate in DB
item_count = column_property(
select(func.count(OrderItem.id))
.where(OrderItem.order_id == id)
.correlate_except(OrderItem)
.scalar_subquery()
)
# Usage: filter on hybrid property
expensive_orders = session.scalars(
select(Order).where(Order.total > 1000)
).all()
# column_property auto-loaded with the model
order = session.get(Order, 1)
print(order.item_count) # Loaded in same SELECT as order
🔍 Interviewer Notes: Tests knowledge of the three property types and when to use each.
❌ Common Mistakes:
- Using
@propertywhen filtering on the server side is needed - Using
column_propertywith an N+1 correlated subquery for every row in a list - Forgetting the
@hybrid_property.expressiondecorator for SQL-side filtering
Q284: How do you migrate from synchronous to async SQLAlchemy with minimal code changes?
Technical Answer:
Use the greenlet_spawn compatibility shim, or migrate incrementally: keep sync code using engine.begin() and migrate endpoints one at a time to async. Use run_sync as an intermediate step for sync-heavy ORM code.
Code Example:
# Phase 1: Create both sync and async engines
from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
# Legacy sync engine (keep during migration)
sync_engine = create_engine("postgresql://user:pass@host/db")
# New async engine (add for async endpoints)
async_engine = create_async_engine("postgresql+asyncpg://user:pass@host/db")
AsyncSessionFactory = async_sessionmaker(async_engine, expire_on_commit=False)
# Phase 2: Async wrapper using run_sync for sync-heavy ORM code
async def get_user_with_legacy_code(user_id: int) -> User:
async with AsyncSessionFactory() as session:
def _sync_query(sync_session):
# Legacy sync code works here unchanged
user = sync_session.get(User, user_id)
_ = user.posts # Lazy loading OK in run_sync
return user
return await session.run_sync(_sync_query)
# Phase 3: Full async migration
async def get_user_async(session: AsyncSession, user_id: int) -> User:
return await session.scalar(
select(User)
.options(selectinload(User.posts))
.where(User.id == user_id)
)
# Phase 4: Async-first FastAPI dependency
async def get_async_db() -> AsyncSession:
async with AsyncSessionFactory() as session:
yield session
@router.get("/users/{user_id}")
async def get_user_endpoint(user_id: int, session: AsyncSession = Depends(get_async_db)):
user = await get_user_async(session, user_id)
if not user:
raise HTTPException(404)
return UserDTO.model_validate(user)
🔍 Interviewer Notes: Tests migration strategy knowledge and incremental modernization approach.
❌ Common Mistakes:
- Switching everything to async at once (high risk, big bang migration)
- Using
asyncio.run()inside an already-running event loop - Not updating
expire_on_commit=Falsefor async sessions
Q285: How do you implement a SQL query builder that prevents SQL injection?
Technical Answer:
Always use SQLAlchemy's expression API instead of string interpolation. Validate dynamic column names against a whitelist. Use literal_column() only for trusted, validated identifiers.
Code Example:
from sqlalchemy import literal_column, asc, desc, text
from sqlalchemy.sql import ColumnElement
ALLOWED_SORT_COLUMNS = {"name", "email", "created_at", "updated_at"}
ALLOWED_SORT_DIRS = {"asc", "desc"}
def build_safe_query(
session: Session,
filters: dict,
sort_by: str = "created_at",
sort_dir: str = "desc",
page: int = 1,
per_page: int = 20,
) -> list[User]:
# Validate sort column against whitelist (prevents injection)
if sort_by not in ALLOWED_SORT_COLUMNS:
raise ValueError(f"Invalid sort column: {sort_by}")
if sort_dir not in ALLOWED_SORT_DIRS:
raise ValueError(f"Invalid sort direction: {sort_dir}")
# Build filter conditions safely
conditions = []
for key, value in filters.items():
if key == "name":
conditions.append(User.name.ilike(f"%{value}%"))
elif key == "email":
conditions.append(User.email == value) # Exact match
elif key == "is_active":
conditions.append(User.is_active == bool(value))
# Unknown filter keys are silently ignored (safe default)
# Build order clause from validated string
col = getattr(User, sort_by)
order_clause = col.asc() if sort_dir == "asc" else col.desc()
return session.scalars(
select(User)
.where(*conditions)
.order_by(order_clause)
.offset((page - 1) * per_page)
.limit(per_page)
).all()
🔍 Interviewer Notes: Tests SQL injection prevention with dynamic queries — a critical security topic.
❌ Common Mistakes:
- Using
text(f"ORDER BY {sort_by}")with user-providedsort_by(SQL injection) - Trusting
getattr(User, user_input)without validating against a whitelist - Silently accepting unknown filter keys instead of ignoring or rejecting them
Q286: How do you implement a database-level JSON schema validation?
Technical Answer:
Use PostgreSQL's JSON Schema validation via check constraints with jsonb_matches_schema() (PostgreSQL 16+) or the pg_jsonschema extension. For older versions, validate at the application layer before insert.
Code Example:
from sqlalchemy import CheckConstraint, JSON, text
# PostgreSQL 16+ with pg_jsonschema extension
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(200))
attributes: Mapped[dict] = mapped_column(JSON)
__table_args__ = (
CheckConstraint(
text("""jsonb_matches_schema(
'{
"type": "object",
"required": ["color", "size"],
"properties": {
"color": {"type": "string"},
"size": {"enum": ["S", "M", "L", "XL"]},
"weight_kg": {"type": "number", "minimum": 0}
}
}',
attributes
)"""),
name="ck_products_attributes_schema"
),
)
# Application-level validation (works with any PostgreSQL version)
from pydantic import BaseModel, validator
from typing import Literal
class ProductAttributes(BaseModel):
color: str
size: Literal["S", "M", "L", "XL"]
weight_kg: float | None = None
def create_product(session: Session, name: str, raw_attrs: dict) -> Product:
# Validate at application layer
attrs = ProductAttributes(**raw_attrs) # Raises ValidationError on invalid input
product = Product(name=name, attributes=attrs.model_dump(exclude_none=True))
session.add(product)
session.commit()
return product
🔍 Interviewer Notes: Tests JSON validation strategies and defense-in-depth for schema enforcement.
❌ Common Mistakes:
- Storing arbitrary JSON without any schema (schema drift over time)
- Only validating at the API layer (direct DB writes bypass validation)
- Using
pg_jsonschemain development but not in production
Q287: How do you implement a database connection lifecycle in a serverless environment?
Technical Answer:
Serverless functions (Lambda, Cloud Functions) have ephemeral execution contexts. Use NullPool or StaticPool to avoid pool overhead. Initialize the engine outside the handler for reuse across warm invocations.
Code Example:
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool, StaticPool
import os
# Module-level: initialized once per cold start, reused across warm invocations
_engine = None
_Session = None
def get_engine():
global _engine, _Session
if _engine is None:
db_url = os.environ["DATABASE_URL"]
# Option 1: NullPool — no connection reuse (safest for serverless)
_engine = create_engine(
db_url,
pool=NullPool,
connect_args={"connect_timeout": 5}
)
# Option 2: StaticPool for SQLite (tests/local)
# _engine = create_engine("sqlite:///:memory:", pool=StaticPool)
_Session = sessionmaker(bind=_engine)
return _engine, _Session
# Lambda handler
def handler(event, context):
engine, Session = get_engine()
with Session() as session:
try:
result = process_event(session, event)
session.commit()
return {"statusCode": 200, "body": result}
except Exception as e:
session.rollback()
return {"statusCode": 500, "body": str(e)}
# For async Lambda (Python 3.12+ / asyncio)
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
async_engine = None
async def async_handler(event, context):
global async_engine
if async_engine is None:
async_engine = create_async_engine(
os.environ["DATABASE_URL"].replace("postgresql://", "postgresql+asyncpg://"),
pool_size=1, max_overflow=0, pool_timeout=5
)
async with AsyncSession(async_engine) as session:
return await process_async(session, event)
🔍 Interviewer Notes: Tests knowledge of SQLAlchemy in serverless architectures and connection management constraints.
❌ Common Mistakes:
- Creating a new engine inside the handler (new connection per invocation — slow, exhausts DB connections)
- Using large pool sizes in Lambda (each concurrent invocation holds its own connections)
- Not handling connection timeouts (Lambda functions time out waiting for slow DB)
Q288: How do you implement a read replica failover with SQLAlchemy?
Technical Answer: Maintain a list of replica endpoints with health checks. Use a round-robin or least-connections strategy for load balancing. Automatically fall back to the primary when all replicas are unavailable.
Code Example:
import random
from sqlalchemy import create_engine, text
from sqlalchemy.exc import OperationalError
class ReplicaRouter:
def __init__(self, primary_url: str, replica_urls: list[str]):
self.primary = create_engine(primary_url, pool_pre_ping=True, pool_size=5)
self.replicas = [
create_engine(url, pool_pre_ping=True, pool_size=10)
for url in replica_urls
]
self._unhealthy = set()
def get_read_engine(self):
healthy = [
(i, e) for i, e in enumerate(self.replicas)
if i not in self._unhealthy
]
if not healthy:
return self.primary # Fall back to primary
# Random selection among healthy replicas
idx, engine = random.choice(healthy)
return engine
def mark_unhealthy(self, engine):
for i, e in enumerate(self.replicas):
if e is engine:
self._unhealthy.add(i)
# Schedule health check after 30s
break
def health_check_replicas(self):
for i in list(self._unhealthy):
try:
with self.replicas[i].connect() as conn:
conn.execute(text("SELECT 1"))
self._unhealthy.discard(i)
except Exception:
pass
router = ReplicaRouter(
primary_url=os.environ["PRIMARY_DB_URL"],
replica_urls=os.environ["REPLICA_DB_URLS"].split(",")
)
def get_read_session():
engine = router.get_read_engine()
session = Session(engine)
try:
yield session
except OperationalError as e:
router.mark_unhealthy(engine)
raise
finally:
session.close()
🔍 Interviewer Notes: Tests production resilience patterns for read-heavy workloads.
❌ Common Mistakes:
- Reading from replica immediately after writing to primary (replication lag)
- Not implementing replica health checks (stuck on failed replica indefinitely)
- Not falling back to primary when all replicas are down
Q289: How do you implement a database queue with priority levels using SQLAlchemy?
Technical Answer:
Use a jobs table with a priority column. Implement SKIP LOCKED for concurrent workers and multiple priority levels. Higher-priority jobs run first within each worker cycle.
Code Example:
from sqlalchemy import Integer, String, JSON, DateTime, Index, text
from enum import IntEnum
class JobPriority(IntEnum):
LOW = 10
NORMAL = 50
HIGH = 100
CRITICAL = 1000
class Job(Base):
__tablename__ = "jobs"
id: Mapped[int] = mapped_column(primary_key=True)
queue: Mapped[str] = mapped_column(String(50), default="default")
priority: Mapped[int] = mapped_column(default=JobPriority.NORMAL)
payload: Mapped[dict] = mapped_column(JSON)
status: Mapped[str] = mapped_column(String(20), default="pending")
attempts: Mapped[int] = mapped_column(default=0)
scheduled_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
locked_at: Mapped[Optional[datetime]] = mapped_column(default=None)
locked_by: Mapped[Optional[str]] = mapped_column(String(100), default=None)
__table_args__ = (
Index("ix_jobs_dequeue", "queue", "status", "priority", "scheduled_at",
postgresql_where=(text("status = 'pending'"))),
)
def dequeue(session: Session, queue: str = "default", worker_id: str = "worker-1") -> Optional[Job]:
job = session.scalars(
select(Job)
.where(
Job.queue == queue,
Job.status == "pending",
Job.scheduled_at <= datetime.utcnow(),
)
.order_by(Job.priority.desc(), Job.scheduled_at.asc())
.limit(1)
.with_for_update(skip_locked=True)
).first()
if job:
job.status = "processing"
job.locked_at = datetime.utcnow()
job.locked_by = worker_id
session.commit()
return job
def complete_job(session: Session, job: Job):
job.status = "completed"
session.commit()
def fail_job(session: Session, job: Job, retry_delay_seconds: int = 60):
job.attempts += 1
if job.attempts >= 3:
job.status = "failed"
else:
job.status = "pending"
job.scheduled_at = datetime.utcnow() + timedelta(seconds=retry_delay_seconds)
job.locked_at = None
job.locked_by = None
session.commit()
🔍 Interviewer Notes: Tests database-backed queue patterns for task processing systems.
❌ Common Mistakes:
- Not using
SKIP LOCKED(multiple workers process the same job) - Not implementing retry logic with backoff (failed jobs loop immediately)
- Not having a
scheduled_atfield (can't implement delayed jobs)
Q290: How do you implement a complete SQLAlchemy model lifecycle with all patterns?
Technical Answer: A production SQLAlchemy model combines: TimestampMixin, SoftDeleteMixin, audit events, versioning, hybrid properties, validators, and proper relationship configuration. This is the complete pattern for enterprise applications.
Code Example:
from sqlalchemy.orm import (
DeclarativeBase, Mapped, mapped_column, relationship,
validates, declared_attr
)
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import String, Boolean, DateTime, Integer, JSON, event, func
class Base(DeclarativeBase):
pass
class TimestampMixin:
@declared_attr
def created_at(cls) -> Mapped[datetime]:
return mapped_column(DateTime(timezone=True), server_default=func.now())
@declared_attr
def updated_at(cls) -> Mapped[datetime]:
return mapped_column(DateTime(timezone=True), server_default=func.now(),
onupdate=func.now())
class SoftDeleteMixin:
@declared_attr
def deleted_at(cls) -> Mapped[Optional[datetime]]:
return mapped_column(DateTime(timezone=True), nullable=True, default=None)
def soft_delete(self):
self.deleted_at = datetime.now(timezone.utc)
class VersionMixin:
@declared_attr
def version(cls) -> Mapped[int]:
return mapped_column(Integer, default=1)
@declared_attr
def __mapper_args__(cls):
return {"version_id_col": cls.__dict__.get("version")}
class User(TimestampMixin, SoftDeleteMixin, Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
email: Mapped[str] = mapped_column(String(200), unique=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
settings: Mapped[dict] = mapped_column(JSON, default=dict)
posts: Mapped[List["Post"]] = relationship(
"Post", back_populates="author",
lazy="select", cascade="all, delete-orphan"
)
@validates("email")
def validate_email(self, key, value):
import re
if not re.match(r"[^@]+@[^@]+\.[^@]+", value):
raise ValueError(f"Invalid email: {value}")
return value.lower().strip()
@hybrid_property
def display_name(self) -> str:
return self.name
@display_name.expression
def display_name(cls):
return cls.name
def __repr__(self) -> str:
return f"<User(id={self.id}, email={self.email!r})>"
# Register global events
@event.listens_for(User, "before_insert")
def set_defaults(mapper, connection, target):
if not target.settings:
target.settings = {"theme": "light", "notifications": True}
@event.listens_for(Session, "do_orm_execute")
def filter_deleted(execute_state):
if execute_state.is_select:
for entity in execute_state.statement.column_descriptions:
if hasattr(entity.get("entity"), "deleted_at"):
execute_state.statement = execute_state.statement.filter(
entity["entity"].deleted_at.is_(None)
)
🔍 Interviewer Notes: This is the capstone question — tests the ability to compose multiple patterns into a coherent production model.
❌ Common Mistakes:
- Mixing concerns in a single large model file instead of using mixins
- Not declaring
__mapper_args__viadeclared_attrin mixins (doesn't inherit correctly) - Over-engineering simple models with all patterns regardless of actual requirements
Complete Index
Core Fundamentals
Engine setup, session management, ORM CRUD, relationships, eager/lazy loading, transactions, Alembic, inspect, sqlalchemy_utils
Intermediate
Async SQLAlchemy, bulk operations, connection pooling, query optimization, testing, PostgreSQL-specific features, advanced Alembic, RLS, CQRS
Advanced
Custom types, sharding, CDC outbox, saga, bi-temporal, circuit breaker, full observability, schema-per-tenant, temporal tables, complete integration patterns
Q291: How do you use SQLAlchemy with asyncpg vs psycopg3 (psycopg)?
Technical Answer:
asyncpg is a fast pure-async PostgreSQL driver (no sync fallback). psycopg3 (psycopg) supports both sync and async with similar API to psycopg2. Key differences: asyncpg uses named prepared statements by default (disable for pgBouncer); psycopg3 has better type system integration.
Code Example:
# asyncpg (SQLAlchemy 1.4+)
from sqlalchemy.ext.asyncio import create_async_engine
engine_asyncpg = create_async_engine(
"postgresql+asyncpg://user:pass@host/db",
# asyncpg caches prepared statements — disable for pgBouncer
connect_args={"statement_cache_size": 0},
pool_size=10,
)
# psycopg3 (psycopg) — SQLAlchemy 2.0+
engine_psycopg = create_async_engine(
"postgresql+psycopg://user:pass@host/db",
# psycopg3 doesn't cache statements by default
pool_size=10,
)
# psycopg3 has native Python type coercion
# asyncpg requires codec registration for custom types
# Register custom asyncpg codec for UUID
from sqlalchemy import event
import uuid
@event.listens_for(engine_asyncpg.sync_engine, "connect")
def register_custom_types(dbapi_conn, connection_record):
import asyncpg
# asyncpg handles UUID natively — no registration needed
pass
# psycopg3: better support for new PostgreSQL types
engine_psycopg3_sync = create_engine(
"postgresql+psycopg://user:pass@host/db",
# psycopg3 uses binary protocol by default (faster)
)
🔍 Interviewer Notes: Tests knowledge of async driver options and their trade-offs.
❌ Common Mistakes:
- Using asyncpg with pgBouncer without disabling statement cache
- Mixing sync and async operations on asyncpg engine
- Not knowing that psycopg3 replaces psycopg2 for new projects
Q292: How do you implement polymorphic search across multiple entity types?
Technical Answer:
Use a UNION ALL across entity-specific views or tables, with a discriminator column. Alternatively, maintain a shared search_index table populated by triggers or application events.
Code Example:
from sqlalchemy import union_all, literal, select, String
# Approach 1: UNION ALL across entities
def global_search(session: Session, query: str) -> list:
user_stmt = (
select(
User.id,
literal("user").label("entity_type"),
User.name.label("title"),
User.email.label("subtitle"),
)
.where(func.similarity(User.name, query) > 0.3)
)
product_stmt = (
select(
Product.id,
literal("product").label("entity_type"),
Product.name.label("title"),
Product.description.label("subtitle"),
)
.where(func.similarity(Product.name, query) > 0.3)
)
combined = union_all(user_stmt, product_stmt).subquery()
results = session.execute(
select(combined).order_by(combined.c.title).limit(20)
).all()
return results
# Approach 2: Shared search index table
class SearchIndex(Base):
__tablename__ = "search_index"
id: Mapped[int] = mapped_column(primary_key=True)
entity_type: Mapped[str] = mapped_column(String(50))
entity_id: Mapped[int] = mapped_column()
title: Mapped[str] = mapped_column(String(300))
body: Mapped[Optional[str]] = mapped_column(Text)
search_vector = Column(TSVECTOR)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
__table_args__ = (
Index("ix_search_index_fts", "search_vector", postgresql_using="gin"),
UniqueConstraint("entity_type", "entity_id", name="uq_search_index_entity"),
)
🔍 Interviewer Notes: Tests cross-entity search architecture design.
❌ Common Mistakes:
- Using UNION ALL without indexes on the underlying tables
- Not normalizing search index (duplication of business data)
- Not handling deletes in the search index (stale results)
Q293: How do you implement row-level permissions beyond RLS?
Technical Answer:
Combine PostgreSQL RLS with application-level permission checks for complex rules (e.g., team membership, role hierarchies). Use a permissions table with (user_id, resource_type, resource_id, action) for fine-grained ACL.
Code Example:
class Permission(Base):
__tablename__ = "permissions"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
resource_type: Mapped[str] = mapped_column(String(50))
resource_id: Mapped[int] = mapped_column()
action: Mapped[str] = mapped_column(String(20)) # read, write, delete, admin
granted_by: Mapped[int] = mapped_column(ForeignKey("users.id"))
granted_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
__table_args__ = (
UniqueConstraint("user_id", "resource_type", "resource_id", "action",
name="uq_permissions"),
Index("ix_permissions_lookup", "user_id", "resource_type", "resource_id"),
)
def has_permission(session: Session, user_id: int, resource_type: str,
resource_id: int, action: str) -> bool:
return session.scalars(
select(func.count(Permission.id))
.where(
Permission.user_id == user_id,
Permission.resource_type == resource_type,
Permission.resource_id == resource_id,
Permission.action.in_([action, "admin"]), # admin grants all
)
).one() > 0
# Efficient: check permissions during query (filter at DB level)
def get_accessible_documents(session: Session, user_id: int) -> list:
return session.scalars(
select(Document)
.join(
Permission,
and_(
Permission.user_id == user_id,
Permission.resource_type == "document",
Permission.resource_id == Document.id,
Permission.action.in_(["read", "admin"]),
)
)
).all()
🔍 Interviewer Notes: Tests fine-grained authorization design and efficient permission checking.
❌ Common Mistakes:
- Checking permissions in a loop for each document (N+1)
- Not caching permission checks for hot paths
- Not having an
adminaction that implies all other actions
Q294: How do you implement a distributed counter with SQLAlchemy?
Technical Answer:
Use atomic UPDATE ... RETURNING for a correct distributed counter without race conditions. For high-write scenarios, use Redis for counting with periodic DB sync, or PostgreSQL's advisory_count approach with batched increments.
Code Example:
from sqlalchemy import text
from sqlalchemy.dialects.postgresql import insert as pg_insert
class Counter(Base):
__tablename__ = "counters"
name: Mapped[str] = mapped_column(String(100), primary_key=True)
value: Mapped[int] = mapped_column(default=0)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def increment_counter(session: Session, name: str, by: int = 1) -> int:
"""Atomic increment. Returns new value."""
stmt = (
pg_insert(Counter)
.values(name=name, value=by, updated_at=datetime.utcnow())
.on_conflict_do_update(
index_elements=["name"],
set_={"value": Counter.value + by, "updated_at": datetime.utcnow()}
)
.returning(Counter.value)
)
new_value = session.execute(stmt).scalar()
session.commit()
return new_value
def get_counter(session: Session, name: str) -> int:
return session.scalars(
select(Counter.value).where(Counter.name == name)
).first() or 0
# High-throughput: batch increments using Redis
import redis
redis_client = redis.Redis()
def fast_increment(name: str, by: int = 1) -> int:
"""Increment via Redis, sync to DB periodically."""
return redis_client.incrby(f"counter:{name}", by)
def sync_counters_to_db(session: Session, counter_names: list[str]):
"""Sync Redis counters to DB (run periodically)."""
pipeline = redis_client.pipeline()
for name in counter_names:
pipeline.getdel(f"counter:{name}")
values = pipeline.execute()
for name, value in zip(counter_names, values):
if value:
increment_counter(session, name, int(value))
🔍 Interviewer Notes: Tests distributed counter patterns and atomic operations.
❌ Common Mistakes:
- Using read-then-write pattern (race condition under concurrency)
- Not using
RETURNINGto get the new value atomically - Not syncing Redis counters before reading them from DB
Q295: How do you implement a polymorphic ORM query with dynamic dispatch?
Technical Answer:
Use session.get() with the base class to retrieve polymorphic objects, then use isinstance() for type-specific logic. Avoid type(obj).__name__ for dispatch — use the polymorphic identity or a method on the model.
Code Example:
from typing import Union
class Notification(Base):
__tablename__ = "notifications"
__mapper_args__ = {"polymorphic_on": "type", "polymorphic_identity": "base"}
id: Mapped[int] = mapped_column(primary_key=True)
type: Mapped[str] = mapped_column(String(30))
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
def render(self) -> str:
raise NotImplementedError
class EmailNotification(Notification):
__tablename__ = "email_notifications"
__mapper_args__ = {"polymorphic_identity": "email"}
id: Mapped[int] = mapped_column(ForeignKey("notifications.id"), primary_key=True)
subject: Mapped[str] = mapped_column(String(200))
body: Mapped[str] = mapped_column(Text)
def render(self) -> str:
return f"Email: {self.subject}"
class PushNotification(Notification):
__tablename__ = "push_notifications"
__mapper_args__ = {"polymorphic_identity": "push"}
id: Mapped[int] = mapped_column(ForeignKey("notifications.id"), primary_key=True)
title: Mapped[str] = mapped_column(String(100))
device_token: Mapped[str] = mapped_column(String(200))
def render(self) -> str:
return f"Push: {self.title}"
# Query returns correctly typed subclass instances
def get_user_notifications(session: Session, user_id: int) -> list[Notification]:
return session.scalars(
select(Notification)
.where(Notification.user_id == user_id)
.options(selectin_polymorphic(Notification, [EmailNotification, PushNotification]))
.order_by(Notification.created_at.desc())
).all()
def send_all(session: Session, user_id: int):
notifications = get_user_notifications(session, user_id)
for notif in notifications:
print(notif.render()) # Virtual dispatch via polymorphism
🔍 Interviewer Notes: Tests polymorphic ORM patterns and virtual dispatch in Python.
❌ Common Mistakes:
- Using
if type(obj).__name__ == "Email"instead ofisinstance() - Not using
selectin_polymorphic(issues N+1 JOINs per subtype) - Forgetting to define
render()on all subclasses (silent None returns)
Q296: How do you implement custom SQLAlchemy column types for domain values?
Technical Answer:
Create TypeDecorator subclasses for domain-specific types: money (stored as integer cents), country code (validated string), email (normalized). Each type handles both Python representation and database storage transparently.
Code Example:
from sqlalchemy import TypeDecorator, Integer, String
from decimal import Decimal
class MoneyType(TypeDecorator):
"""Stores money as integer cents, exposes as Decimal."""
impl = Integer
cache_ok = True
def process_bind_param(self, value, dialect):
if value is None:
return None
if isinstance(value, Decimal):
return int(value * 100)
return int(float(value) * 100)
def process_result_value(self, value, dialect):
if value is None:
return None
return Decimal(value) / Decimal(100)
class CountryCodeType(TypeDecorator):
"""Uppercase ISO 3166-1 alpha-2 country code."""
impl = String(2)
cache_ok = True
def process_bind_param(self, value, dialect):
if value is None:
return None
code = str(value).upper().strip()
if len(code) != 2 or not code.isalpha():
raise ValueError(f"Invalid country code: {value}")
return code
def process_result_value(self, value, dialect):
return value
class Order(Base):
__tablename__ = "orders"
id: Mapped[int] = mapped_column(primary_key=True)
total = Column(MoneyType, nullable=False)
shipping_country = Column(CountryCodeType, nullable=False)
# Usage
order = Order(total=Decimal("49.99"), shipping_country="us")
session.add(order)
session.flush()
# Stored as: total=4999 (integer), shipping_country="US"
# Retrieved as: total=Decimal('49.99'), shipping_country="US"
# Filter using domain values directly
expensive = session.scalars(
select(Order).where(Order.total > Decimal("100.00"))
).all()
🔍 Interviewer Notes: Tests TypeDecorator patterns for domain modeling.
❌ Common Mistakes:
- Storing money as float (floating point precision errors)
- Not setting
cache_ok = True(disables query plan caching) - Not handling edge cases in
process_bind_param(None, different numeric types)
Q297: How do you implement a streaming ETL pipeline with SQLAlchemy?
Technical Answer:
Use yield_per() for memory-efficient source reading and bulk insert with execute(insert(), list_of_dicts) for efficient writing. Process in pipeline stages using Python generators to keep memory bounded.
Code Example:
from typing import Iterator, Callable
from sqlalchemy import insert
def read_source(session: Session, batch_size: int = 1000) -> Iterator[list]:
stmt = select(SourceRecord).where(SourceRecord.processed == False)
stmt = stmt.execution_options(yield_per=batch_size)
async with session.stream_scalars(stmt) as stream:
async for partition in stream.partitions(batch_size):
yield partition
def transform(records: list[SourceRecord]) -> list[dict]:
return [
{
"id": r.id,
"normalized_name": r.name.strip().title(),
"amount_cents": int(r.amount * 100),
"processed_at": datetime.utcnow(),
}
for r in records
]
def write_target(session: Session, records: list[dict]):
if not records:
return
session.execute(
insert(TargetRecord).on_conflict_do_update(
index_elements=["id"],
set_={k: insert(TargetRecord).excluded[k] for k in records[0].keys()}
),
records
)
async def run_etl(source_session: AsyncSession, target_session: AsyncSession):
processed = 0
async for batch in read_source(source_session, batch_size=500):
transformed = transform(batch)
write_target(target_session, transformed)
await target_session.commit()
# Mark source records as processed
ids = [r.id for r in batch]
await source_session.execute(
SourceRecord.__table__.update()
.where(SourceRecord.id.in_(ids))
.values(processed=True)
)
await source_session.commit()
processed += len(batch)
print(f"ETL complete: {processed} records processed")
🔍 Interviewer Notes: Tests data pipeline design and memory-efficient processing patterns.
❌ Common Mistakes:
- Loading all source records into memory before processing
- Writing records one-by-one instead of bulk insert
- Not committing source records as processed (re-processes on failure)
Q298: How do you implement a database session middleware for FastAPI with proper cleanup?
Technical Answer:
Use FastAPI's dependency injection with a generator function. The session is created per request, yielded for use, and closed/rolled back in the finally block. For async, use asynccontextmanager and async_sessionmaker.
Code Example:
from contextlib import asynccontextmanager
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from fastapi import Depends, Request
import time
engine = create_async_engine(DATABASE_URL, pool_pre_ping=True)
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
# Standard async dependency
async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
# With request-scoped state for observability
async def get_db_with_metrics(request: Request) -> AsyncSession:
start = time.monotonic()
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
elapsed = time.monotonic() - start
if elapsed > 0.5:
import logging
logging.getLogger("slow_request").warning(
"Slow DB session: %.1fms path=%s",
elapsed * 1000,
request.url.path
)
await session.close()
# Router usage
@router.post("/users")
async def create_user(
data: UserCreate,
session: AsyncSession = Depends(get_db)
):
user = User(name=data.name, email=data.email)
session.add(user)
await session.flush() # Get ID before response
return {"id": user.id, "email": user.email}
🔍 Interviewer Notes: Tests production FastAPI + SQLAlchemy session management patterns.
❌ Common Mistakes:
- Not wrapping session yield in try/finally (session not closed on exception)
- Committing inside every endpoint instead of in the dependency (inconsistent behavior)
- Using a global session shared across requests (not thread/coroutine safe)
Q299: How do you implement cascading updates with SQLAlchemy ORM?
Technical Answer:
Use cascade="all, delete-orphan" for full parent→child cascade. For update cascading (e.g., denormalized data), use after_update events or database-level ON UPDATE CASCADE. SQLAlchemy cascades only deletions and session operations by default.
Code Example:
from sqlalchemy import event, ForeignKey
from sqlalchemy.orm import relationship
class Category(Base):
__tablename__ = "categories"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
full_path: Mapped[str] = mapped_column(String(500))
parent_id: Mapped[Optional[int]] = mapped_column(ForeignKey("categories.id"))
parent: Mapped[Optional["Category"]] = relationship(
"Category", remote_side="Category.id", back_populates="children"
)
children: Mapped[List["Category"]] = relationship(
"Category", back_populates="parent",
cascade="all, delete-orphan" # ORM: cascades delete and merge ops
)
# Custom cascade: update full_path when name changes
@event.listens_for(Category, "after_update")
def cascade_path_update(mapper, connection, target):
from sqlalchemy.orm import attributes
hist = attributes.get_history(target, "name")
if not hist.has_changes():
return
# Update all descendants' full_path
connection.execute(
text("""
WITH RECURSIVE descendants AS (
SELECT id, parent_id, name, :new_prefix || name AS new_path
FROM categories WHERE id = :id
UNION ALL
SELECT c.id, c.parent_id, c.name, d.new_path || '/' || c.name
FROM categories c
JOIN descendants d ON c.parent_id = d.id
)
UPDATE categories SET full_path = d.new_path
FROM descendants d WHERE categories.id = d.id
"""),
{"id": target.id, "new_prefix": target.full_path.rsplit("/", 1)[0] + "/"}
)
# DB-level FK cascade for hard deletes
class OrderItem(Base):
__tablename__ = "order_items"
order_id: Mapped[int] = mapped_column(
ForeignKey("orders.id", ondelete="CASCADE", onupdate="CASCADE"),
primary_key=True
)
🔍 Interviewer Notes: Tests cascade configuration knowledge and update propagation patterns.
❌ Common Mistakes:
- Confusing SQLAlchemy ORM cascade (session operations) with DB FK cascade (DDL)
- Using
cascade="all"withoutdelete-orphan(orphans remain after parent deletion) - Implementing recursive updates in Python (use a recursive CTE instead)
Q300: What are the most important SQLAlchemy best practices for production applications?
Technical Answer:
The top production best practices: (1) use pool_pre_ping, (2) set expire_on_commit=False for async, (3) use selectinload for collections, (4) always use bound parameters, (5) version all schema changes via Alembic, (6) log slow queries, (7) use lazy="raise" in tests.
Code Example:
# Production engine setup checklist
engine = create_async_engine(
DATABASE_URL,
# 1. Pool health
pool_pre_ping=True,
pool_recycle=3600,
pool_size=10,
max_overflow=20,
# 2. Connection timeout
connect_args={"connect_timeout": 10},
# 3. Query logging (conditional)
echo=False,
# 4. pgBouncer compat
# connect_args={"statement_cache_size": 0}, # asyncpg
)
session_factory = async_sessionmaker(
engine,
expire_on_commit=False, # 5. Prevent lazy load after commit
class_=AsyncSession,
)
# Development model: catch lazy loads
class User(Base):
posts: Mapped[List["Post"]] = relationship(
"Post",
lazy="raise" if os.getenv("ENV") == "development" else "select"
)
# Alembic best practices
# - Always name constraints: name="ck_...", "fk_...", "uq_...", "ix_..."
# - Run CONCURRENTLY for indexes in production
# - Test both upgrade and downgrade in CI
# - Use advisory locks for concurrent deployment
# Query best practices
stmt = (
select(User)
.options(selectinload(User.posts)) # Avoid N+1
.where(User.is_active == True) # Use bound params (automatic)
.order_by(User.created_at.desc())
.limit(20) # Always LIMIT
)
# Monitoring best practices
@event.listens_for(engine.sync_engine, "after_cursor_execute")
def log_slow_queries(conn, cursor, stmt, params, context, executemany):
elapsed = time.monotonic() - conn.info.get("_start", time.monotonic())
if elapsed > 0.1:
logging.warning("SLOW: %.0fms — %s", elapsed * 1000, stmt[:100])
🔍 Interviewer Notes: The capstone best-practices question. Tests breadth of knowledge and production experience.
❌ Common Mistakes:
- Not using
pool_pre_ping(stale connections cause random errors) - Using
expire_on_commit=Truein async (unexpected lazy loads) - No slow query monitoring (performance issues invisible until SLA breach)
- Not naming constraints (impossible to drop/modify them in migrations)
- Loading collections without eager loading (N+1 in production)
End of SQLAlchemy Core Interview Guide — 300 Questions