User.where(email: …) returns typed objects without a line of SQL, and early in a project that is most of what an ORM is asked to do. The usual word for this is abstraction, as if the database were now a detail hidden behind the model. But an abstraction lets one side change without the other noticing, and an ORM does the opposite. It describes the schema a second time, in code, and from then on a column can’t be added, renamed or retyped on one side without the matching edit on the other. That is a coupling: a bidirectional contract between schema and code that both sides have to honor for every change.
The contract shapes more than how changes propagate. It also shapes the schema itself, because an ORM’s default output is a database structured like the class graph rather than one designed for the workload. Short-lived prototypes and simple CRUD apps still benefit from ORMs. The defensible use cases are narrower than the industry’s default deployment pattern suggests, and the coupling is durable and consistently underestimated at the point a team decides to adopt one.
What the ORM is actually doing
The word “ORM” suggests abstraction, “object-relational mapping” as if the mapping is the hidden plumbing. The practical reality is the opposite: the mapping is the product. An ORM takes your schema shape and projects it onto code shape. Columns become fields. Tables become classes. Foreign keys become methods. Indexes are invisible until you care about them. Constraints are whatever the ORM’s DSL exposes and nothing more.
That projection is useful. It lets application code avoid SQL, most of the time. It also means the code and schema are now two views of the same data model, and those views are expected to stay in sync by you, by your migration framework, by your tests, and by every developer who touches either side.
Stay in sync, in practice, means every schema change is also a code change and every migration is a coordinated edit across multiple files. That coupling is the defining characteristic of the tool.
Every ORM ecosystem has a default answer to “where does the schema canonically live”, and most teams never think about it. Rails and Django are model-first: migrations are generated from changes to model classes, the model is the source of truth, and the schema follows. (Running rails db:schema:dump produces a schema.rb that describes the current state, and the migration files are the history of how it got there.) sqlc and jOOQ are schema-first: they read SQL DDL files and generate typed client code, so the schema is the source of truth and the code follows. Hibernate can do either, depending on configuration, and SQLAlchemy lets you declare models in Python and generate migrations via Alembic, or point Alembic at an existing schema and generate models. Teams that don’t decide end up doing both.
The undecided case is where the real damage happens. Over years, a team that migrates from model-first to schema-first (or vice versa) without a clean cutover ends up with a schema that neither the models nor the migration history correctly describes. Rows backfilled by a DBA with direct SQL don’t show up in the ORM’s understanding of the world. Columns added by a production hotfix get rediscovered six months later when someone regenerates models from the database. The fix is to decide, document, and enforce whichever one you pick, the way you would any other convention.
In a raw-SQL codebase, a schema migration is a single file: CREATE TABLE, ALTER TABLE, DROP COLUMN. The migration is the change.
In an ORM codebase, a single logical schema change is typically:
- A migration file (
add_email_to_users.rb). - The model class (
User#emailgetter, validation,serializecalls). - The serializer (
UserSerializer#email). - The API contract (OpenAPI spec, GraphQL schema, whatever the team uses).
- Fixtures and factories (FactoryBot, factory_boy, test data).
- Query helpers that need to know the new column.
- Type stubs or generated types (TypeScript declarations, Python stubs).
- Admin UI config, sometimes.
What should be a single metadata-level change is now a coordinated edit across five to eight files, and missing any one of them produces a subtly broken application. The ORM didn’t create the complexity; it distributed it. The schema change is still one change. It just has to be propagated to every place the code has a mirror of the schema.
At small scale this is fine. The friction compounds once the team is big enough that the people writing the migration aren’t the same people owning the serializers and the API consumers. A schema change now requires coordinating across teams, each with their own view of the data model, each needing their files updated. The schema itself didn’t get harder to change. The ORM layer around it did.
Hidden queries
The ORM generates SQL you didn’t write. That’s the value proposition. It’s also a persistent failure mode.
Lazy loading is the classic. user.orders triggers a query, user.orders.first.line_items triggers another, and a loop over 100 users runs at least 101 queries, none of them visible in the code. Eager loading exists (.includes(:orders)) but only if someone remembers to write it. The default is lazy. Defaults win. Magic methods hide their multiplicity: where(status: :active).first_or_create(email: …) is three or four queries depending on the code path, and the code says nothing about it. Generated sorts do the same for indexes, since User.order(:created_at).limit(10) on a table without an index on created_at is a full table scan the reviewer never saw.
All of this is the ORM doing exactly what it said it would. The cost is that the SQL the database actually runs isn’t in version control, isn’t code-reviewed, and isn’t profiled until it shows up in slow-query logs. Every ORM codebase accumulates query shapes nobody intentionally wrote.
pg_stat_statements, EXPLAIN on every slow path) just to know what’s actually running.Once queries go beyond creating, reading, updating and deleting single rows, every ORM codebase ends up with raw SQL living alongside ORM calls. Window functions, recursive CTEs, PostgreSQL DISTINCT ON, LATERAL joins, MySQL INSERT ... ON DUPLICATE KEY UPDATE with complex update clauses, exclusion constraints, full-text search, spatial queries: the list of things awkward or impossible to express through the ORM grows over the life of the project.
The result is a codebase with two query languages coexisting. Reviewers have to know both. Type safety is uneven; ORM calls produce typed objects, raw SQL produces hashes or arrays that need manual mapping. The two styles drift. The ORM-side queries follow the ORM’s conventions; the raw-SQL queries follow whatever the author happened to write that day.
Past a certain complexity threshold the ORM stops reducing the SQL surface area and starts adding a layer on top of it. The SQL didn’t go away. It got pushed into the half of the codebase that’s harder to trace.
SELECT.Bidirectional coupling
The part that surprises teams is how hard it is to leave.
Migrating a database schema (renaming a column, changing a type, splitting a table) is mechanical. It’s a migration file and a deploy window. The mechanics are well-understood and the blast radius is bounded.
Migrating off an ORM is not mechanical, because the conventions have bled everywhere. Controller and API code returns JSON whose shape matches model attributes, with as_json, serializable_hash, and callbacks defining what the outside world sees. The test suite’s fixtures, factories, and in-memory SQLite databases depend on the ORM being there. Third-party integrations (export formats, webhooks, analytics pipelines) were built against the ORM’s JSON representation of the data, and admin UIs like Rails Admin, Django Admin, and Laravel Nova are hard-wired to its conventions. Every scope, association, and callback in the codebase is ORM-native. Every engineer who’s been there more than a year thinks in its abstractions.
The database is the innocent party here; it’s the surrounding code that grew up expecting the ORM to be there, and replacing the ORM means replacing or rewriting every one of those layers. A schema migration is bounded work; replacing the ORM is not. The coupling is bidirectional, but the two directions don’t cost the same to undo, and teams that adopt an ORM for velocity rarely account for the exit cost.
Database-side logic doesn’t round-trip
Most ORMs have a tunnel-vision view of the schema: they see what they created. They don’t see:
CHECKconstraints. The ORM has no concept of them. A constraint likeCHECK (amount >= 0)is invisible to the model; the ORM’s validations become the only gatekeeper the application knows about.- Triggers. A trigger that mutates a row after insert produces data the ORM didn’t know would be there. Reading back the row often requires an explicit reload.
- Generated columns. MySQL’s
GENERATED ALWAYS AS (…) STOREDand PostgreSQL’s equivalent produce values the ORM treats as regular columns, but they can’t be written to, and the ORM’s default behavior is to try. - Partial and expression indexes. The ORM sees the column, not the index. A query that should hit a partial index on
WHERE deleted_at IS NULLgets generated without that predicate and misses the index. - Exclusion constraints. PostgreSQL
EXCLUDE USING gist (…). Completely outside the ORM’s worldview.
The ORM’s view of the schema is a subset of the real schema. Queries written against that subset can violate invariants the database enforces. The application code thinks the write succeeded; the INSERT comes back with a constraint violation; the code has no idea why. Teams paper over this with application-level validation that duplicates the database’s, and then the two drift, which is its own class of production incident.
The class graph shapes the schema
One direction of the coupling is easy to see: schema changes require code changes. It also runs the other way, which is harder to see. The ORM’s object model is what shapes the schema in the first place. For simple data, a User with an email and a password hash, that’s fine. For non-trivial domains the result is a schema that looks like a class hierarchy, and the gap has a name, the object-relational impedance mismatch: tables are structured by the relationships between entities and the way the workload reads them, classes by inheritance.
Consider a scheduling application with three kinds of entries: appointments, days off, and product launches. All of them are events with a start time, an owner and a status, and each has different additional fields. The relational answer is a supertype/subtype pattern (sometimes called class table inheritance): a base events table with the shared fields, and one table per subtype whose event_id is both its primary key and a foreign key back to events. Each subtype has its own columns, indexes and constraints, and a new field on appointments doesn’t touch events, days_off or launches.
The ORM-driven shape tends to be something different. Rails’ single-table inheritance (STI) collapses everything into one wide table with a type column and every possible subtype field nullable. Django’s multi-table inheritance is closer to the relational answer but introduces implicit joins the developer didn’t ask for. Hibernate offers all three strategies (SINGLE_TABLE, JOINED, TABLE_PER_CLASS), and SINGLE_TABLE is the default. In the single-table shape, adding a field to one subtype means adding a nullable column visible to every other subtype, and indexes have to include the type column to be useful (God Tables covers the wide table itself). Many-to-many bridges with their own columns, polymorphic references and recursive self-references pull the same way: the ORM’s answer is usually a custom association that looks natural in code and generates SQL nobody would write by hand.
The class-shaped schema is cheap at small scale and its cost stays hidden until the workload grows. By then the schema shape is coupled to the class graph the application assumes, so fixing it takes an application restructure and not a schema migration. What tends to happen instead is infrastructure that papers over the shape: read replicas, caching layers, materialized views. Each has legitimate uses, and none of them is a fix for a schema that wasn’t designed for the access pattern it’s getting.
The thinner alternatives
There’s a spectrum between “hand-roll every query with database/sql” and “full ORM with an identity map (the per-session cache that hands back the same object for the same row), lazy loading, and 200-line models.” Several tools occupy the middle ground by treating SQL as the source of truth and generating typed code from it, without the mapping layer.
- sqlc. Go, Kotlin, Python, TypeScript. You write SQL queries in
.sqlfiles; sqlc generates type-safe client code. The schema is canonical, the queries are code-reviewed SQL, and there’s no runtime layer to reason about. Migrations stay plain DDL. - jOOQ. JVM. Reads your schema and produces a fluent, type-safe DSL for building queries. Feels like SQL, reads like SQL, with compile-time type checking. Schema-first, no model mapping.
- Kysely. TypeScript. Typed query builder with no ORM layer. You describe the schema in types; Kysely ensures queries match. The full SQL surface area is reachable.
- Drizzle. TypeScript. Despite the name, closer to a typed query builder than a classical ORM. Schema declared in code, queries written in a SQL-like DSL, no identity map.
- Plain
database/sqlorpgxwith a small query helper. Go in particular has a tradition of “raw SQL plus a thin wrapper.” More boilerplate, minimal coupling.
The common thread across these tools: schema is the source of truth, queries are code-reviewed first-class artifacts, and there’s no mapping layer pretending the database doesn’t exist. The payoff is predictability; the SQL you see is the SQL that runs. The cost is some of the magic: no User.find(1).orders.where(total: 100..).first_or_create one-liners.
For long-lived OLTP systems with non-trivial query shapes, that predictability is worth more than the magic. For short-lived CRUD apps, it isn’t.
ORMs have a place, and it’s narrower than the industry’s default deployment suggests. The workloads where the velocity payoff consistently outweighs the coupling cost share two properties: bounded scope and bounded lifespan. Short-lived prototypes and experiments qualify, because model-first iteration is faster while the schema is fluid and the coupling cost never gets time to compound. CRUD-heavy internal tools and admin UIs qualify too: the query shapes are uniform and simple, and the system doesn’t outlive the product it supports. The justifications teams actually use (“the team knows Rails”, “the query shapes are simple, for now”, “we’re small”) aren’t on that list. Those framings start as short-lived exceptions and end up as the default, and the failure mode is keeping the ORM ten years later, after the prototype has become the company’s main production system.
The counter-arguments are real. ORMs save time on simple queries; User.find(1) is shorter than SELECT * FROM users WHERE id = 1, and across a codebase it adds up. Type safety in the application layer is a genuine benefit where the ecosystem delivers it (Django’s model fields, SQLAlchemy’s typed columns, and Hibernate’s entity types do; Rails and ActiveRecord don’t give compile-time types), and raw SQL’s answer, schema-first code generation, works but requires tooling. Some teams legitimately want their data model to have methods, validations, and behavior co-located with the data. An ORM gives that for free; a query builder doesn’t. A team that knows Rails deeply will out-ship a team learning sqlc for the same project. And the middle ground has costs of its own: typed query builders require maintained type definitions, schema-first code generation adds a build step, and “no ORM” means a different abstraction, maintained by you.
Inside an existing ORM codebase the signals are specific: the raw-SQL ratio creeping up, migrations that require cross-team coordination, queries the ORM can’t express, predicates it wraps in a function so the index goes unused, performance paths that bypass it anyway. When they show up, the useful question is where to draw the schema-first boundary for new work. Usually at new subsystems, not legacy code. Grandfather what’s there, pick up sqlc or jOOQ or Kysely for new code, and let the boundary move over years.
