Basics

What is an ORM — working with a database without writing SQL

Illustration: a translator passing a note between a person at a laptop and a shelf of tables

Here's a handy thing: to save a user to the database, you write user.save(). One line. No INSERT INTO users (...) VALUES (...), none of the quotes and commas everyone trips over. As if the database understood your code directly.

It doesn't. Between those two worlds stands a translator — it's called an ORM. And until you know what it does behind the scenes, it can quietly turn one of your loops into a hundred trips to the database. Let's unpack this middleman and where its catch is.

What an ORM means in plain words

ORM stands for Object-Relational Mapping. Sounds scary, but the idea is simple.

In code you think in objects: a user that has a name and an email. In the database the same data lives as table rows: a row in the users table, columns name, email. Two different ways of storing the same thing. An ORM is a library that translates back and forth: it turns your user object into a table row, and a row from the database back into an object.

An analogy: you speak your language, the database speaks SQL. The ORM is the translator between you. You dictate "save this user," it composes and sends the correct SQL query on the fly, and it brings the database's answer back to you as a tidy object.

What it looks like: code instead of SQL

The same action — "give me the user with id 7" — in two forms.

  • Raw SQL: SELECT * FROM users WHERE id = 7; — and you still have to unpack the result into fields by hand.
  • Through an ORM: User.find(7) — and you immediately get a ready object user with fields user.name, user.email.

The ORM wrote that same SELECT itself, went to the database itself, parsed the answer itself. You work in your app's language without switching to the database's. Popular ORMs: Prisma and Drizzle in JavaScript, SQLAlchemy in Python, Active Record in Ruby.

What it's for — and why not always

Three honest reasons to love an ORM:

  • Less busywork. Simple operations — create, read, update, delete — are one line instead of a long query.
  • Fewer dangerous mistakes. The ORM escapes your data for you, which closes off a whole class of holes — SQL injection, where malicious code is smuggled into a query through an input field.
  • One language across the project. No flipping between code and SQL in your head ten times a day.

But the translator has a price. It adds a layer — sometimes it generates a less-than-optimal query. And it hides the SQL: while everything's fast, you never think about what's happening under the hood. When it gets slow, you'll have to go down to the real queries anyway and see what the ORM cooked up. So it's worth knowing basic SQL even if you write through an ORM: this SQL vs NoSQL comparison is a good foundation.

The catch: the N+1 problem

Here's the classic trap that AI-written ORM code leads a beginner into most often.

You grab 100 posts and want to show each author's name. In a loop you write, for each post, post.author.name. Looks innocent. But behind every post.author the ORM quietly fires a separate query to the database. The result: 1 query for the posts + 100 queries for the authors = 101 trips where two would do. That's the N+1 problem: one query breeds N more.

The user feels this as "the page takes forever." The fix is to ask the ORM to fetch the authors up front, in one query (in different ORMs this is include, join, or eager loading). But to even notice the problem, you need to see how many queries went out — and that's exactly what the ORM hides.

FAQ

Does an ORM replace knowing SQL?

No, it postpones it. You can start a project with an ORM without knowing SQL — that's fine. But the first serious slow page will send you to read the real queries the ORM generated. Basic SQL is insurance: without it you won't understand what to fix.

Is an ORM a database?

No. A database stores data; an ORM is just a library in your code that talks to that database. Remove the ORM and the database stays put — you'll just have to write the queries by hand in SQL.

Do I always need an ORM?

No. For a small project or a couple of simple queries, raw SQL can be both shorter and clearer. An ORM pays off when you have many models, many relationships between them, and lots of routine operations — that is, on a medium-to-large app.

Learn vibe coding — don’t just read about it

Short story-lessons, an agent simulator and daily practice — in our mobile app. Free.

Open the app
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →