SeaORM, Diesel and Django ORM, Oh My! Picking an ORM Without Getting Lost on the Yellow Brick Road
Lions and tigers and bears, oh my! Except it's SeaORM, Diesel and Django ORM, and the forest is your database. The same query in all three, what each one is best at, and the N+1 problem that eats all of them alive.
Ada Brackett
5 min read
You're walking down the yellow brick road towards the Emerald City of Working Software. The forest gets dark. Something rustles in the trees.
SeaORM and Diesel and Django, oh my!
An ORM (object-relational mapper) lets you talk to your database with your language's own types and methods instead of hand-writing every SQL string. Today we put three popular ones side by side: SeaORM vs Diesel in Rust, and the Django ORM in Python. The same query in each, what they're good at, and the one monster that lives in every forest.
The query
We'll fetch the ten newest published posts from a posts table. That's simple, but it shows each one's personality.
Django ORM: the friendly wizard
Python, part of the Django web framework, and arguably the most beginner-friendly ORM around.
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Post(models.Model):
title = models.CharField(max_length=200)
published = models.BooleanField(default=False)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
recent = Post.objects.filter(published=True).order_by("-id")[:10]
That's it. Read it out loud and it's nearly English.
- Migrations are automatic. Change a model, run
python manage.py makemigrationsandpython manage.py migrate, and Django writes and applies the schema change for you. - QuerySets are lazy. That line doesn't touch the database until you loop over it, the way Rust iterators wait until something consumes them.
- It comes with friends. The admin site, forms and authentication all plug straight into your models.
- Async support has been growing over recent releases, but much of the Django ecosystem is still sync-first.
Best for: web apps where you want to move fast, and anything where the built-in admin saves you a month.
Diesel: the strict but fair Tin Man
Rust, and the longest-established Rust ORM. Diesel's big idea: if your query is wrong, it doesn't compile.
Diesel's CLI manages migrations written in SQL, and generates a schema.rs describing your tables. Then:
use diesel::prelude::*;
#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::schema::posts)]
struct Post {
id: i32,
title: String,
published: bool,
}
fn recent_posts(conn: &mut PgConnection) -> QueryResult<Vec<Post>> {
use crate::schema::posts::dsl::*;
posts
.filter(published.eq(true))
.order(id.desc())
.limit(10)
.select(Post::as_select())
.load(conn)
}
Misspell a column? Compare a bool to a string? Select columns that don't match your struct? Compiler error, before anything ever runs. It's like having a database administrator living inside cargo build.
- Sync by default. Diesel's core API is synchronous. The separate
diesel-asynccrate adds async connections if you need them. - Very fast, with little overhead between you and the SQL.
- The trade-off: the type errors can be long and scary when you're starting out. The Tin Man has a heart. It's just buried under three screens of generic type parameters.
Best for: Rust services where correctness matters most and you're happy to learn its query language.
SeaORM: the async Scarecrow (who definitely has a brain)
Rust, async from the ground up, and built on top of SQLx. SeaORM's CLI can generate entity files straight from your existing database (sea-orm-cli generate entity), and it has its own migration tool.
use sea_orm::{ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter, QueryOrder, QuerySelect};
async fn recent_posts(db: &DatabaseConnection) -> Result<Vec<post::Model>, DbErr> {
post::Entity::find()
.filter(post::Column::Published.eq(true))
.order_by_desc(post::Column::Id)
.limit(10)
.all(db)
.await
}
(post is the module SeaORM generates for the table.)
- Async-native. It's a natural fit for Tokio and web frameworks like Axum.
- Dynamic queries are easy. Building a query from user-chosen filters at run time is more comfortable than in Diesel.
- Feels familiar if you've used ORMs in other languages: entities, models and an
ActiveModelfor inserts and updates. - The trade-off: more mistakes are caught at run time instead of at compile time, compared with Diesel.
Best for: async Rust web services, especially ones with flexible, filter-heavy queries.
The monster in every forest: N+1
Here's the scary bit, and no ORM saves you from it automatically. Say you list ten posts and their authors' names:
for post in Post.objects.filter(published=True)[:10]:
print(post.title, post.author.name)
That looks innocent. It's actually eleven queries: one for the posts, then one more for each author, because each post.author quietly goes back to the database. With a thousand posts, that's a thousand and one queries, and your page takes a coffee break.
The fix in Django is one word:
Post.objects.filter(published=True).select_related("author")[:10]
That's one query with a join. (Use prefetch_related for many-to-many and reverse relations.) Diesel and SeaORM have their own answers (Diesel's joins and belonging_to, SeaORM's related-entity loaders), but the lesson is the same everywhere: an ORM makes queries easy to write, which also makes them easy to write a thousand of. Turn on query logging in development and count.
The map out of the forest
| Django ORM | Diesel | SeaORM | |
|---|---|---|---|
| Language | Python | Rust | Rust |
| Async | Growing, sync-first | Via diesel-async | Native |
| Query mistakes caught | At run time | At compile time | Mostly at run time |
| Migrations | Generated from models | SQL files, CLI | Its own migrator |
| Learning curve | Gentle | Steep, then smooth | Moderate |
| Feels like | A friendly wizard | A strict professor | A flexible toolkit |
There's no place like home
- Building a web app fast, in Python? Django ORM. Click your heels and go.
- A Rust service where wrong queries must never ship? Diesel.
- An async Rust web service with Tokio and Axum? SeaORM.
- Want to write the SQL yourself but still have it checked? Look at SQLx, which isn't an ORM at all. Its
query!macro checks plain SQL against your real database at compile time.
And whichever you pick: learn the SQL underneath anyway. Every ORM eventually generates a query you need to understand. When the curtain gets pulled back, the wizard is always just SQL.
Databases
More like this
Databases
Postgres uuidv7 in PostgreSQL 18: Time-Ordered Primary Keys in Ten Minutes
PostgreSQL 18 ships a built-in uuidv7() function, so time-ordered UUID primary keys need no extension. A ten-minute lab: make a table, read the timestamp back out, backfill with a shift, and know when not to use them.
Databases
The Journey to the Holy Grail, Part 4: Camelot
Camelot was the first pool built from golden images. Getting a site onto it took an OpenSSH surprise, a Python decision, a Postgres permission rule, a missing systemd unit that took our own test site down for hours, and a Postgres 16 to 18 upgrade.