Skip to content

ORM and when to write raw SQL

Introduction

An ORM lets you operate the database with objects and methods in code, so you rarely write SQL by hand; but it is not a cure-all, and complex queries should be written in SQL.

Why use it

  • Less boilerplate: everyday CRUD rarely needs hand-written SQL.
  • Safer: parameterized queries by default, which avoids SQL injection (attackers stuffing malicious SQL into input fields).
  • Works with migrations: the model is the schema; change it in one place.

How to do it

  1. Use the ORM for everyday CRUD.
  2. Watch for the N+1 problem: one query returns 100 orders, then each order triggers another query for its user — 101 queries. Fix it with eager loading (fetch associations in one go).
  3. For complex reports, multi-table aggregates, and bulk updates, write SQL, but always parameterize — never concatenate strings.
  4. Select only the columns you need; do not always load entire rows or tables.
  5. Turn on SQL logging in development and inspect what the ORM actually sends.

Component comparison

EF Core

Built into the frameworkOpsLow

.NET's official ORM (object-relational mapping: operate database tables through code objects, without writing most SQL).

Best forMost C# business systems, especially CRUD-centric applications.

Avoid whenYou are extremely sensitive to query performance, or have many complex report SQL queries.

Pros

Ordinary create, read, update, and delete can be written in C#

Migrations are built in, so schema changes are recorded

It is maintained officially, with plenty of docs and samples

Cons

SQL generated for a complex query may not be efficient enough

Without care, one page can fire dozens of queries

The overhead is higher than hand-written SQL or Dapper

Official docs ↗

Dapper

Self-hosted open sourceOpen sourceOpsLow

A lightweight .NET library: you write the SQL yourself, and it loads query results into C# objects.

Best forC# projects needing fine control over SQL, or paired with EF Core for complex queries.

Avoid whenThe team is not familiar with SQL, or the schema changes frequently.

Pros

It is fast and light, and you fully control the SQL

Very few concepts; after the docs you can start writing

Complex queries and statistical reports are straightforward to write

Cons

You maintain the SQL yourself, and switching databases may mean rewriting it

There is no built-in migration, so schema changes need another approach

When a column is renamed, the compiler will not catch a wrong SQL string

Official docs ↗

SQLAlchemy

Self-hosted open sourceOpen sourceOpsLow

Python's most commonly used general-purpose ORM and database toolkit, often paired with frameworks such as FastAPI and Flask.

Best forPython projects using FastAPI or Flask.

Avoid whenYou already use Django, where its built-in ORM is easier.

Pros

Supports both object-style operation and near-SQL expressions, so it is flexible

Supports many databases

Not tied to any web framework

Cons

Many concepts (session, engine, relationship loading strategies), easy to misuse

Schema migrations need a separate tool such as Alembic

Without care it issues a large number of queries

Official docs ↗

Django ORM

Built into the frameworkOpsLow

The ORM built into the Django framework, integrated with the admin, forms, and migrations.

Best forWebsites, admin backends, and e-commerce projects built with Django.

Avoid whenThe project does not use the Django framework.

Pros

Ready to use, and it works together with the built-in admin

Migrations are built in, so changing the schema is less work

The query style is intuitive, and tutorials and material are plentiful

Cons

It is bound to Django, so changing frameworks means a rewrite

Especially complex queries feel awkward and often fall back to SQL

Without care, one action issues a large number of queries

Official docs ↗

Hand-written SQL

Hand-rolledOpen sourceOpsLow

No ORM: write SQL statements directly through the database driver to operate on data.

Best forComplex, performance-sensitive queries, or small tools with very few tables.

Avoid whenBusiness systems centered on ordinary CRUD, where the team lacks SQL experience.

Pros

You control query performance and behavior yourself

Complex reports and statistical queries are the most direct to write

You do not add an ORM as another layer of dependency

Cons

Repeated SQL is common, and more columns make mistakes easier

Queries must be parameterized, or there is an injection risk

When the schema changes, you have to update the matching SQL yourself

Prisma

Open-source libraryOpen sourceOpsLow

A schema-file-centric TypeScript ORM whose query results are fully typed.

Best forMost CRUD-centric TypeScript backends.

Avoid whenLots of complex SQL, or when serverless cold-start size is critical.

Pros

Type-safe with a great autocomplete experience

The schema doubles as documentation, with an integrated migration tool

Quick to pick up with a mature ecosystem

Cons

Complex SQL and reporting queries need raw queries

The generated client and runtime are heavy

Limited support for database-specific features

Official docs ↗

Drizzle ORM

Open-source libraryOpen sourceOpsLow

A lightweight, SQL-like TypeScript ORM that defines table structure in code.

Best forProjects whose team knows SQL and wants lightweight, controllable tooling.

Avoid whenThe team wants heavy abstraction and less SQL.

Pros

SQL-like syntax with strong control

Small footprint, suitable for serverless

Good type inference with no code generation

Cons

Relatively young, with fast-changing versions

The syntax for relational queries takes getting used to

Fewer resources than Prisma

Official docs ↗

SQLx

Open-source libraryOpen sourceOpsLow

An async database library that can check SQL at compile time, without an ORM's implicit loading.

Best forRust services that prefer explicit SQL.

Avoid whenYou want less SQL and automatic relationship loading.

Pros

SQL stays under your control, with optional compile-time checks

Async and a connection pool are built in

The most common pairing with Axum

Cons

Dynamic filters have to be assembled by you

Relationship loading is hand-written

Compile-time checks need a database connection or offline data

Official docs ↗

SeaORM

Open-source libraryOpen sourceOpsLow

An async ORM with entities, relations, and migrations, suited to CRUD-heavy Rust projects.

Best forCRUD-heavy projects that want to write less SQL.

Avoid whenYou need tight control of SQL and compile-time checks.

Pros

Entity and relation style is close to common ORMs

Ships with a migration tool

Async, and works with Axum or Actix

Cons

Complex SQL still falls back to raw queries

Generated code and macros take time to learn

Smaller community than SQLx

Official docs ↗

sqlc

Open-source libraryOpen sourceOpsLow

Write SQL and get type-safe Go code generated automatically, with no runtime ORM layer.

Best forGo services that prefer explicit SQL and value maintainability.

Avoid whenLots of dynamic filter conditions, or you want to build queries in an object-oriented way.

Pros

Controllable SQL with type-safe generated code

No runtime reflection, so performance is good

Pairs cleanly with migration tools

Cons

Dynamic conditional queries are awkward to write

You must maintain SQL files and the generation workflow

Does not provide ORM features such as relationship loading

Official docs ↗

GORM

Open-source libraryOpen sourceOpsLow

The most popular ORM in Go, supporting associations, hooks, and auto-migration.

Best forRapid development and CRUD-centric projects.

Avoid whenYou need high control over SQL and strong performance.

Pros

Full-featured and quick to pick up

The largest community and the most resources

Convenient association loading and transaction wrapping

Cons

Reflection and the chained API make it easy to write inefficient SQL

Auto-migration is not suitable for production

Lots of implicit behavior, so debugging is costly

Official docs ↗

Checklist

  • Everyday CRUD uses the ORM; complex analytics may use SQL
  • List queries have been checked for N+1
  • Every raw SQL query uses parameters — no string concatenation
  • Dev/test environments can show the SQL that actually runs
  • Slow queries are tuned together with indexes