Skip to main content
AllDevToolsHub
Back to Glossary

Object-Relational Mapping (ORM)

A technique that lets you query and manipulate data from a database using an object-oriented paradigm.

Detailed Explanation

ORMs (like Prisma, Hibernate, or Eloquent) allow developers to interact with a database using their language's native syntax instead of writing raw SQL. While they improve developer speed and provide type safety, they can sometimes lead to inefficient queries (like the 'N+1 problem') if not used carefully. They are standard in most modern web backend frameworks.

Quick Summary

An ORM lets you read and write database rows as language objects instead of writing SQL strings. It speeds up everyday CRUD and adds type safety, but the abstraction leaks at scale, you still need to understand the SQL underneath.

Key Takeaways

Key Takeaways

  • Common ORMs: Prisma, Drizzle (TS), SQLAlchemy, Django ORM (Python), Hibernate, JPA (Java), ActiveRecord (Ruby), Eloquent (PHP), EF Core (.NET).
  • Query builders (Knex, Kysely, jOOQ) sit between raw SQL and full ORMs, type-safe queries without object mapping.
  • The N+1 query problem is the classic ORM footgun: looping over results and lazy-loading a related row per iteration.
  • Migrations are usually managed by the ORM and worth treating with care; they're production-critical code.
  • Drop to raw SQL for analytics, bulk operations, and hot paths the ORM compiles poorly.
Use Cases

When to use it

  • Most CRUD-heavy backend apps where developer velocity outweighs raw query control.
  • Type-safe data layers in TypeScript with Prisma or Drizzle.
  • Schema-driven development with code-generated types matching the database.
  • Cross-database portability (development on SQLite, production on PostgreSQL).
Watch out

Common Mistakes

  • N+1 queries from naive iteration, use eager loading (`include`, `joinedload`) or batch fetching.
  • Treating the ORM as a SQL replacement instead of a layer, engineers who can't read EXPLAIN miss the real performance issues.
  • Long-running transactions held open across slow business logic, blocking other writes.
  • Schema drift between ORM models and the actual database, especially when migrations are run manually.
FAQ

Object-Relational Mapping (ORM), Frequently Asked

ORM, query builder, or raw SQL?

Use an ORM for 80% of typical CRUD. Drop to query builder when the ORM gets in the way. Drop to raw SQL for analytical queries, bulk operations, and database-specific features. Most teams use all three in the same codebase.

What's the N+1 problem?

When loading a list of N items and then issuing one query per item to fetch its related data, you've issued N+1 queries instead of one. The fix is eager loading via the ORM's join/include API. Most ORMs have a query log mode that surfaces N+1s in development.