# pgdorm > Typed Python 3.15+ ORM with a compiled Rust core targeting PostgreSQL 18+. Zero raw SQL execution, 1-RTT pipelined wire protocol, and constant SQL shapes. ## Quick Start - Install: `uv add pgdorm` (requires Python 3.15+ and PostgreSQL 18+). - Connect: `async with await pg.Pool.connect(dsn) as pool:` - Define tables: subclass `pg.Table` (or `pg.TypeTable` for `*Type` lookup enums). - Raw Markdown twins: append `.md` to any page URL or read `llms-full.txt`. ## The 5 House Rules 1. **100% PostgreSQL 18+**: Baseline is `server_version_num >= 180_000`; newer-server features are capability-gated. 2. **Zero raw SQL execution**: `query_raw`, `execute_raw`, `pg.sql`, `pg.raw`, `pg.RunSql` do not exist. Use typed ORM primitives (`pg.select`, `pg.insert`, `pg.update`, `pg.delete`, `pg.merge`, `pg.func`, `pg.cast`, `pg.lit`, `pg.select_expr`). 3. **1-RTT & constant SQL shape**: Use `col.in_any(vals)` (`= ANY($1)`), `pg.insert_many()` (`UNNEST WITH ORDINALITY`), and `pool.pipeline()` so PostgreSQL caches one prepared statement per shape. 4. **Strict 1-to-1 naming**: Class `OrderItem` maps to `order_item` (no `name=` overrides). Permanent tables declare `_id` (`Col[int]` -> `BIGINT`; `TypeTable` -> `INTEGER` + unique `code`). 5. **FK & Join hygiene**: `auto_index_fk = True` by default; symmetric FKs emit `JOIN ... USING (...)`; ambiguous or unindexed joins fail fast with `JoinLintError`. ## Error -> Fix - `QueryBuildError`: Invalid query/DML AST -> fix builder arguments or types. - `SchemaLintError`: Table/PK/TypeTable naming violation -> use singular `snake_case`, `_id`, and `*Type` suffix. - `JoinLintError`: Ambiguous or unindexed FK join -> specify explicit `on=`, add index, or `.allow_seq_scan(reason=...)`. - `UnsafeDmlError`: `UPDATE`/`DELETE` without `WHERE` -> add `.where(...)` or `.allow_unfiltered()`. - `NoResultError` / `MultipleResultsError`: `.one()` cardinality mismatch -> use `.one_or_none()` or `.all()`. - `UniqueViolationError` / `ExclusionViolationError`: Constraint conflict -> handle or use `.on_conflict_do_update()`. ## Get Started - [Before Your First Shift](https://wergstatt.gitlab.io/pgdorm/guide/first-shift/): 60-Second Quickstart & 5 House Rules - [House Rules & Error Fixes](https://wergstatt.gitlab.io/pgdorm/guide/house-rules-and-errors/): Symptom-to-Fix Hub for Lint & Runtime Errors - [Coming From Another ORM?](https://wergstatt.gitlab.io/pgdorm/guide/coming-from-other-orms/): SQLAlchemy, Django, Prisma & SQLModel Map - [Building Libraries on pgdorm](https://wergstatt.gitlab.io/pgdorm/guide/library-authors/): Registries, Protocols, Migrations & Testing - [Cloud & Managed Postgres](https://wergstatt.gitlab.io/pgdorm/guide/cloud-postgres/): PgBouncer, Transaction Pooling & Cloud Setup ## The Story - [Story Map](https://wergstatt.gitlab.io/pgdorm/story/) - [Cast](https://wergstatt.gitlab.io/pgdorm/story/cast/) - [Prologue](https://wergstatt.gitlab.io/pgdorm/story/00_prologue/) - [Ch. 1 -- First Check-In](https://wergstatt.gitlab.io/pgdorm/story/01_hello_dorm/): INSERT RETURNING & CRUD - [Ch. 2 -- Partial Updates & Guardrails](https://wergstatt.gitlab.io/pgdorm/story/02_patch_and_returning/): Partial PATCH & Guardrails - [Ch. 3 -- The 01:00 Herd](https://wergstatt.gitlab.io/pgdorm/story/03_bulk_insert_and_upsert/): UNNEST Bulk Insert & Upsert - [Ch. 4 -- A Mammoth at the Kiosk](https://wergstatt.gitlab.io/pgdorm/story/04_column_types_and_ddl/): TypeTable, JSONB & UUIDv7 - [Ch. 5 -- Group Bookings & Folios](https://wergstatt.gitlab.io/pgdorm/story/05_joins_and_folios/): FK Joins, USING & split_load - [Ch. 6 -- No Double Bookings](https://wergstatt.gitlab.io/pgdorm/story/06_temporal_bookings/): WITHOUT OVERLAPS Ranges - [Ch. 7 -- Payments & Chores](https://wergstatt.gitlab.io/pgdorm/story/07_inheritance_payments_tasks/): STI & CTI Polymorphism - [Ch. 8 -- The 3 AM Task Board](https://wergstatt.gitlab.io/pgdorm/story/08_transactions_locks_queue/): Locks, SKIP LOCKED & NOTIFY - [Ch. 9 -- The Endless Guest List](https://wergstatt.gitlab.io/pgdorm/story/09_keyset_pagination_plans/): Keyset & EXPLAIN Plans - [Ch. 10 -- Harbour Wi-Fi](https://wergstatt.gitlab.io/pgdorm/story/10_pipelining_prepared_stream/): 1-RTT Pipelining & Streams - [Ch. 11 -- The Morning Revenue Digest](https://wergstatt.gitlab.io/pgdorm/story/11_analytics_revenue_report/): ROLLUP, Windows & QUALIFY - [Ch. 12 -- Mapping the Empire](https://wergstatt.gitlab.io/pgdorm/story/12_hierarchy_regions_cte/): Recursive CTEs & Set Ops - [Ch. 13 -- The Nightly Feed](https://wergstatt.gitlab.io/pgdorm/story/13_staging_and_merge_import/): Staging & MERGE RETURNING - [Ch. 14 -- Lisbon Can't See Porto](https://wergstatt.gitlab.io/pgdorm/story/14_multitenancy_rls/): RLS Policies & PgBouncer - [Ch. 15 -- A Million Door Scans](https://wergstatt.gitlab.io/pgdorm/story/15_partitioning_door_events/): Range Partitioning - [Ch. 16 -- The Polar Bears Arrive](https://wergstatt.gitlab.io/pgdorm/story/16_arrow_polars_analytics/): Zero-Copy Arrow & Polars - [Ch. 17 -- ETL, Lakehouse & Reverse ETL](https://wergstatt.gitlab.io/pgdorm/story/17_arrow_duckdb_lakehouse/): Arrow C Stream & DuckDB - [Ch. 18 -- The Booking Desk API](https://wergstatt.gitlab.io/pgdorm/story/18_web_api_and_telemetry/): FastAPI, Wire JSON & Stats - [Ch. 19 -- Renovating While Open](https://wergstatt.gitlab.io/pgdorm/story/19_online_migrations/): Lock-Safe Online Migrations - [Ch. 20 -- Moby's Test Lab](https://wergstatt.gitlab.io/pgdorm/story/20_testing_your_app/): 1-RTT & Schema Test Lab - [Epilogue](https://wergstatt.gitlab.io/pgdorm/story/epilogue/) - [1 | Unit of Work & Identity Map](https://wergstatt.gitlab.io/pgdorm/story/field_notes_uow_and_identity_map/) - [2 | TypeTable vs. ENUM Evolution](https://wergstatt.gitlab.io/pgdorm/story/field_notes_type_tables_and_enums/) - [3 | Nested Hydration & Graphs](https://wergstatt.gitlab.io/pgdorm/story/field_notes_nested_hydration/) - [4 | Legacy Assimilation](https://wergstatt.gitlab.io/pgdorm/story/field_notes_legacy_assimilation/) - [5 | Wire Hydration & JSON Modes](https://wergstatt.gitlab.io/pgdorm/story/field_notes_wire_and_json/) - [6 | Search, JSONPath & DML](https://wergstatt.gitlab.io/pgdorm/story/field_notes_search_json_and_dml/) - [Dormitory Schema](https://wergstatt.gitlab.io/pgdorm/story/schema_reference/) - [Zero-SQL Hall of Shame](https://wergstatt.gitlab.io/pgdorm/story/hall_of_shame/): 6 Gnarly 1-RTT PG Queries ## Renovations - [Renovations Map](https://wergstatt.gitlab.io/pgdorm/migrations/) - [Ch. 0 -- The Internship](https://wergstatt.gitlab.io/pgdorm/migrations/00_internship/): Schema From Scratch, Step by Step - [Ch. 1 -- Opening the Blueprints](https://wergstatt.gitlab.io/pgdorm/migrations/01_blueprints_and_sync/): Catalog Diff, Sync & Drift - [Ch. 2 -- Knocking Down Walls Safely](https://wergstatt.gitlab.io/pgdorm/migrations/02_online_ddl_and_hazards/): Online DDL, Locks & Hazards - [Ch. 3 -- Signed Work Orders](https://wergstatt.gitlab.io/pgdorm/migrations/03_versioned_migrations/): Versioned Apps & Checksums - [Ch. 4 -- Moving Elephants Mid-Renovation](https://wergstatt.gitlab.io/pgdorm/migrations/04_backfills_expand_contract/): RunPython & Expand-Contract - [Ch. 5 -- Hard Hats, Locks & Power Cuts](https://wergstatt.gitlab.io/pgdorm/migrations/05_locks_pgbouncer_recovery/): Lock Retries, Leases & Recovery - [Ch. 6 -- Wings, Policies & Living Lookups](https://wergstatt.gitlab.io/pgdorm/migrations/06_advanced_pg_evolution/): TypeTables, RLS & Partitions - [Ch. 7 -- The Foreman's Toolkit](https://wergstatt.gitlab.io/pgdorm/migrations/07_cli_and_ci_gates/): pgdorm CLI, JSON & CI Gates - [Ch. 8 -- Buying the Building Next Door](https://wergstatt.gitlab.io/pgdorm/migrations/08_adopting_legacy_schemas/): Audit, Scaffold & AdoptionMap ## Reference - [API Overview](https://wergstatt.gitlab.io/pgdorm/reference/): Complete pgdorm Public Surface Index - [Models, Columns & Types](https://wergstatt.gitlab.io/pgdorm/reference/models-and-types/): Table, TypeTable, Col, Constraints, Indexes & PG Types - [Queries, Joins & DML](https://wergstatt.gitlab.io/pgdorm/reference/queries-and-dml/): SELECT, Joins, CTEs, INSERT, UPDATE, DELETE & MERGE - [Expressions & Functions](https://wergstatt.gitlab.io/pgdorm/reference/expressions-and-functions/): pg.func, Windows, Aggregates, JSONB, FTS & Ranges - [Runtime, Pool & Wire](https://wergstatt.gitlab.io/pgdorm/reference/runtime-and-pool/): Pool, Transactions, Pipelines, COPY, Arrow & Telemetry - [Errors & Linters](https://wergstatt.gitlab.io/pgdorm/reference/errors-and-linters/): Structured PostgresError & Static Lint Exceptions - [Migrations & Assimilation](https://wergstatt.gitlab.io/pgdorm/reference/migrations-and-assimilation/): pgdorm.migrate Diff, Sync, Online DDL & Legacy Assimilation - [CLI Reference](https://wergstatt.gitlab.io/pgdorm/reference/cli/): All 12 pgdorm CLI Subcommands, Flags & Exit Codes - [Typing & Testing](https://wergstatt.gitlab.io/pgdorm/reference/typing-and-testing/): pgdorm.typing Protocols & pgdorm.testing Helpers - [Glossary](https://wergstatt.gitlab.io/pgdorm/guide/glossary/): Domain Terms & PostgreSQL Wire/ORM Vocabulary ## Assimilation - [Assimilation](https://wergstatt.gitlab.io/pgdorm/assimilation/) ## Benchmarks - [Benchmarks](https://wergstatt.gitlab.io/pgdorm/benchmarks/) ## Proving Grounds - [Proving Grounds](https://wergstatt.gitlab.io/pgdorm/proving/)