Coming From Another ORM?#
Already comfortable with SQLAlchemy 2, SQLModel, Django ORM, Prisma, Supabase SDK, or Psycopg 3? Welcome — this page translates your mental model onto pgdorm in five minutes so you can jump straight to the chapters you care about.
Skeptical that you can build production PostgreSQL apps with zero raw SQL? Read The Zero-SQL Hall of Shame for side-by-side Python, compiled SQL, and live PostgreSQL EXPLAIN proofs, or browse exact signatures in the API Reference.
The three biggest shifts#
- No Session, no identity map, no hidden lazy loads. Models are lightweight
__slots__objects. AGuestnever quietly fires a SQL query when you access an attribute. If you want related rows, ask explicitly with.join(...)orpool.split_load(...)(see Chapter 5), or read the full side-by-side comparison against SQLAlchemy 2AsyncSessionin Field Note 1 · Unit of Work & Identity Map. - PostgreSQL only — by design. Instead of lowest-common-denominator abstractions across five SQL dialects, pgdorm builds directly on modern PostgreSQL primitives (
RETURNING OLD/NEW,WITHOUT OVERLAPS,uuidv7(), virtual generated columns,MERGE … RETURNING,COPY BINARY,LISTEN/NOTIFY,UNNEST WITH ORDINALITY, and= ANY($1)). - Zero raw SQL execution. There is no
text(),raw(), orexecute_raw(). Every feature — from recursive CTEs withSEARCH/CYCLEtoLATERALtop-K,ROLLUP/QUALIFY, RLS policies, and online migrations — is expressed in typed Python so your editor and type checker catches type errors before runtime.
Quick stack comparison (SQLModel, Prisma, Supabase SDK & classic ORMs)#
| Stack | How it works | What changes in pgdorm |
|---|---|---|
| SQLModel | Pydantic + SQLAlchemy Session wrapper |
Models are zero-__dict__ __slots__ classes (no Pydantic overhead on DB reads); pg.Create[T] / pg.Patch[T] generate API DTOs on demand; Rust wire client replaces Python driver + Session tracking. |
| Prisma | External schema DSL (.prisma) + codegen |
Pure Python pg.Table classes are the single source of truth — no separate DSL or codegen step, and native PostgreSQL primitives (MERGE, SKIP LOCKED, WITHOUT OVERLAPS, LATERAL) work without $queryRaw. |
Supabase SDK (postgrest-py) |
HTTP REST calls through PostgREST | Direct binary PostgreSQL v3 wire protocol in Rust: multi-statement transactions (tx.pipeline(commit=True)), FOR UPDATE SKIP LOCKED, pg_temp staging, and local_settings= RLS in 1 RTT (and still works seamlessly against hosted Supabase + Supavisor via pool_mode=pg.enums.PoolMode.TRANSACTION). |
| SQLAlchemy 2 / Django / Psycopg 3 | Unit-of-work ORM or raw SQL strings | Explicit queries with zero implicit lazy loads, constant-shape prepared statements (= ANY($1) / UNNEST), compile-time JoinLintError / UnsafeDmlError guardrails, and 1-RTT pipelining. |
Vocabulary map#
| If you know… | In pgdorm |
Where to see it |
|---|---|---|
Session / AsyncSession |
pg.Pool + async with pool.transaction() as tx: |
Ch. 1, Ch. 8 |
session.add(obj); session.commit() |
await pool.one(pg.insert(T).values(obj).returning(T)) |
Ch. 1 |
bulk_insert_mappings / bulk_create |
pg.insert_many(T, rows) (UNNEST WITH ORDINALITY) |
Ch. 3 |
.where(Col.in_(values)) |
Col.in_any(values) (always = ANY($1)) |
Ch. 3 |
and_(a, b) / or_(a, b) / query composition |
pg.and_(a, b), pg.or_(a, b), and query.pipe(fn) |
Queries & DML Reference |
selectinload() / prefetch_related() |
pool.split_load(parent_q, relations=(...)) (2 RTTs) |
Ch. 5 |
Correlated LATERAL subquery / Top-K per group |
Select.join_lateral(subq, ...) |
Field Note 6 |
UPDATE ... FROM / DELETE ... USING |
pg.update(T).from_(S) / pg.delete(T).using(S) |
Field Note 6, Ch. 13 |
Data-modifying CTE (WITH x AS (UPDATE ... RETURNING ...)) |
pg.write_cte("x", stmt) (WriteCte) |
Field Note 6, Ch. 8 |
exclude_unset=True PATCH boilerplate |
pg.UNSET + pg.Patch[T] + .patch(body) |
Ch. 2, Ch. 18 |
String polymorphic_identity |
pg.discriminator(TypeTable.pk) + variant=Enum.X |
Ch. 7 |
with_for_update(skip_locked=True) |
.for_update(skip_locked=True) |
Ch. 8 |
Multiple sequential awaits |
pool.pipeline() (reads) or tx.pipeline(commit=True) + pg.currval() (1 RTT) |
Ch. 10 |
Server-side json_agg / fast JSON APIs |
await pool.json(q, mode="server") (or mode="wire") |
Ch. 18, Runtime Reference |
| Alembic autogenerate | pgdorm.migrate (plan_migration, sync_schema, check_drift) |
Ch. 19 |
inspectdb / sqlacodegen |
pgdorm audit-schema + pgdorm scaffold + adopt-plan |
Assimilation |
BaseModel Create/Patch DTOs |
pg.Create[T] and pg.Patch[T] |
Ch. 18 |
Pandas read_sql / CSV ETL |
pool.arrow(), copy_to_arrow(), copy_from_arrow() |
Ch. 16, Ch. 17 |
House rules that will surprise you (in a good way)#
- Strict
<table_name>_idPKs: Permanent tables declare<table_name>_id(for instanceguest_id,booking_id). That lets.join(Booking)emitJOIN "booking" USING ("booking_id")automatically — and fail loudly withJoinLintErroron ambiguous or unindexed joins. - No accidental full-table wipes:
pg.delete(Room)orpg.update(Room).set(...)without a.where(...)raisesUnsafeDmlErrorat query-build time (before anything hits the wire) unless you explicitly pass.allow_full_table(reason="..."). - Constant SQL shapes (one prepared statement forever):
in_any(...)always compiles to= ANY($1)andinsert_many(...)always compiles toUNNEST(...). Whether you send 3 items or 3,000, PostgreSQL sees one SQL text, one prepared statement, and one cached plan. (Modern PostgreSQL also squashes variableINlists insidepg_stat_statements; pgdorm gives you the same stability at the wire protocol level and for bulk inserts too.) - PgBouncer-first:
Pool.connect(..., pool_mode=pg.enums.PoolMode.TRANSACTION)handles transaction-pooling constraints cleanly,local_settings=uses transaction-scopedset_config(..., true), andon_commit=pg.enums.OnCommit.DROPkeeps temp staging tables collision-free.
Ready to see how all 20 pieces fit together? Jump into the Story Map, migrate an existing schema in the Assimilation Hub, compare numbers in Benchmarks, or start with the Prologue.
← PreviousHouse Rules & Error FixesSymptom-to-Fix Hub for Lint & Runtime Errors
Next →Building Libraries on pgdormRegistries, Protocols, Migrations & Testing