SeaORM
SeaORM is an async ORM built on SQLx. You define entities, walk relations, and still drop to SQL when the query builder runs out.
Detailed Explanation
SeaORM sits on SQLx's drivers and pool. You get async I/O and an entity model: User, find_by_id, find_related, ActiveModel for inserts and updates. Codegen (sea-orm-cli) writes the entity files from the database. You do not write a Diesel-style schema DSL by hand unless you want to.
The sweet spot is a service that wants ORM convenience — relations, unit-of-work-ish ActiveModel — without leaving Tokio. The cost is another layer between you and SQL. A slow page is still a missing join or an N+1 find_related in a loop. find_with_related and explicit joins are the fix. Logging SQL in development is not optional until you have read the queries.
Transactions use the underlying connection: db.transaction(|txn| Box::pin(async move { ... })). Errors roll back. Nested transactions depend on the engine (savepoints). Do not spawn tasks that use the same transaction handle; the connection is not generally Sync in that way.
SeaORM is younger than Diesel and closer to SQLx. Teams pick it when they want entities and async without Diesel's compile-time DSL. They pick SQLx when the SQL is the product and entities would be theatre.
When you reach for SeaORM: async, entities, relations. When you reach for SQLx: you want to see every query. When you reach for Diesel: sync and a typed DSL you already know.
Migrations are not optional because entities are generated. A generated entity that does not match the live database is a runtime error on the first query. Run the CLI in CI against a migrated schema, or check in both the migration and the entity and review them in the same PR.
Code Examples
struct User {
id: i32,
name: String,
}
async fn find_name(users: &[User], id: i32) -> Option<String> {
users.iter().find(|u| u.id == id).map(|u| u.name.clone())
}
#[tokio::main]
async fn main() {
let users = [User { id: 17, name: "ada".into() }];
println!("{}", find_name(&users, 17).await.unwrap()); // prints: ada
}Common Gotchas
N+1 looks like idiomatic `find_related` in a loop
Load relations in one query. Read the SQL log before you ship a list endpoint.
Entities are generated until you own them
A hand-edit that the next `sea-orm-cli` overwrites is a lost afternoon. Know which files are yours.
The transaction closure is `Send` and `'static` in some APIs
Do not capture a non-`Send` `Rc` into the transaction future.
You can still write raw SQL
When the builder fights you, a raw query is better than seven workarounds.