Databases
What is an ORM?
Definition
An ORM is a library that maps database tables to objects or classes in your language, so records are queried and saved in code rather than in hand-written SQL.
Also known as: Object-Relational Mapping, Object-Relational Mapper
ORM in more detail
A table becomes a class, a row becomes an instance, a foreign key becomes a property you can navigate. The library generates the SQL, converts types in both directions, tracks which loaded objects have changed and writes them back. What you gain is less repetitive code, safety from SQL injection because values are parameterised by default, and a schema that is described in one place alongside migrations.
The trouble is that objects and relations are genuinely different models — the object-relational impedance mismatch — so the abstraction leaks under load. The canonical symptom is the N+1 query: a loop over a hundred orders that touches order.customer issues one query for the list and a hundred more for the customers, none of which is visible in the code. Every mature ORM has an eager-loading facility to fix it, and knowing when to reach for it is the difference between an ORM that helps and one that quietly costs a hundred round trips per page.
The pragmatic position is that an ORM is a productivity tool, not a replacement for understanding the database. Use it for the ordinary create, read, update and delete work that makes up most of an application, and drop to SQL or a query builder for reporting, bulk operations and anything where the plan matters. Log the generated SQL in development — most surprises stop being surprising the moment you can see the statements.
ORM example
# Django ORM
orders = Order.objects.filter(total__gt=20).order_by("-created_at")
# N+1: 1 query for orders, then 1 per order for the customer
for o in orders:
print(o.customer.email)
# Fixed: one extra JOIN instead of N queries
for o in orders.select_related("customer"):
print(o.customer.email)
// Prisma, same problem and the same shape of fix
const orders = await prisma.order.findMany({
where: { total: { gt: 20 } },
include: { customer: true }, // eager load, not N+1
orderBy: { createdAt: "desc" },
});
// When the plan matters, write the SQL
await prisma.$queryRaw`SELECT ... `;ORM — common questions
- What is the N+1 query problem?
- One query loads a list, then accessing a related field on each item issues another query per item — 101 queries for 100 rows. Eager loading (select_related, include, JOIN FETCH) collapses them into one or two.
- Do ORMs make applications slow?
- Not inherently; unexamined query generation does. Most ORM performance problems are N+1 loops, selecting every column when three are needed, or missing indexes — all visible the moment you log the generated SQL.
- Should I use an ORM or write raw SQL?
- Both, in the places each is better. ORMs excel at ordinary CRUD and keep values parameterised; raw SQL wins for reporting queries, bulk updates and anything where you need to control the execution plan.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary