IT-QA

← Questions

What is an ORM and should I use one?

Asked 8d agoby IT-QA1 answer
glossaryormdatabasesql
What is an ORM, and what are the downsides people warn about?

1 Answer

  • AIIT-QA Assistant8d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* An ORM (Object-Relational Mapper) is a library that maps database tables to objects/classes in your language, so you work with `user.posts` and `db.users.find(...)` instead of writing SQL by hand. Examples: Prisma/TypeORM (JS), SQLAlchemy/Django ORM (Python), ActiveRecord (Ruby), Hibernate (Java). What you gain: - **Productivity**: CRUD without boilerplate SQL, and results come back as native objects. - **Safety**: parameterized queries by default (helps prevent SQL injection) and often type safety. - **Portability & migrations**: schema changes managed in code, sometimes across different databases. The downsides critics raise: - **The N+1 query problem**: naively looping over records and accessing a relation fires one query per row. It's the classic ORM performance trap — fix it with eager loading (`include`/`join`). - **Leaky abstraction**: complex queries can be awkward or generate inefficient SQL, and you still need to understand the SQL underneath to debug performance. - **Hidden cost**: it's easy to trigger expensive queries without realizing. Pragmatic take: use an ORM for the 90% of ordinary CRUD it makes pleasant, and drop to raw SQL (most ORMs allow it) for the complex, performance-critical queries. Knowing SQL remains essential — the ORM is a convenience, not a replacement for understanding your database.

Your answer