All posts
May 25, 2026 · Snapdock

What Is an ORM? Why Does My AI Keep Writing Database Code That Looks Like Python?

You built something with Claude, ChatGPT, Cursor, or Bolt that uses a database, and when you look at the code your AI wrote, you expected to see SQL like…

You built something with Claude, ChatGPT, Cursor, or Bolt that uses a database, and when you look at the code your AI wrote, you expected to see SQL like SELECT * FROM users WHERE id = 1. Instead you see something like User.query.filter_by(id=1).first() or db.from(‘users’).select(’*‘).eq(‘id’, 1). Your AI is writing Python or JavaScript that looks like it is talking to the database, but it is not using SQL. This is because it is using an ORM.

What an ORM Actually Is

ORM stands for Object-Relational Mapper. That name is not particularly helpful, so here is what it actually does.

A one-sentence definition: an ORM is a library that lets you interact with your database using the programming language you are already writing, instead of writing SQL queries.

Without an ORM, you write raw SQL: strings of database commands that get sent to the database and executed. SQL works perfectly well but it means switching mental contexts constantly, writing SQL strings inside Python code, and handling all the details of connecting to the database and processing results yourself.

With an ORM, the library translates your Python or JavaScript code into SQL automatically. You write in the language you know, the ORM handles the translation, and you get the results back as regular objects you can work with directly.

Why Your AI Uses ORMs

When your AI builds a database-backed app, it almost always uses an ORM because:

ORMs prevent SQL injection, which is one of the most common security vulnerabilities in web apps. When you write raw SQL with user input, malicious users can inject extra SQL commands and access or delete your data. ORMs handle this protection automatically.

ORMs make code more readable. User.query.filter_by(email=email).first() is clearer to a non-technical reader than SELECT * FROM users WHERE email = ? LIMIT 1.

ORMs handle the database connection, result mapping, and many common operations so your AI does not have to write that boilerplate for every project.

The Most Common ORMs You Will Encounter

SQLAlchemy is the most widely used Python ORM. If your AI-written app uses Python with a relational database, it is almost certainly using SQLAlchemy or its higher-level wrapper Flask-SQLAlchemy.

Prisma is the most popular ORM for JavaScript and TypeScript apps. Clean syntax, automatic type safety, works with most databases. If your Bolt or Lovable app uses a relational database, Prisma is likely involved.

Drizzle is a newer, lighter-weight TypeScript ORM gaining popularity in modern apps.

Django ORM is built into the Django Python framework. If your app uses Django, the ORM is already there and your AI will use it automatically.

When to Know What the ORM Is Doing

You rarely need to look inside the ORM to understand what it is doing. But when something goes wrong, these patterns help:

If your app is running slow database queries, the ORM may be fetching more data than it needs. Ask your AI: “Can you check if my ORM queries are efficient and add select_related or include statements to reduce the number of database queries?”

If you need to do something complex that the ORM makes awkward, your AI can drop to raw SQL for that specific query. Ask: “This query is too complex for the ORM. Can you write it as a raw SQL query instead?”

The One Thing to Remember

An ORM lets you talk to your database using Python or JavaScript instead of SQL. Your AI uses ORMs because they handle security, reduce boilerplate, and make code more readable. SQLAlchemy is common in Python apps, Prisma in JavaScript apps. You do not need to understand ORM internals to work with them. When something goes wrong, paste the relevant code to your AI and describe the problem.


Want your database-powered app running reliably in production? → Snapdock

New here? These might help: What is a database? And does my app actually need one? → What is a database migration? Why your app breaks after you change the schema. →