SeaORM
SeaORM is an async ORM built on sqlx - dynamic queries, relations, and codegen for service-style Rust backends.
Busca en todas las páginas de la documentación
SeaORM is an async ORM built on sqlx - dynamic queries, relations, and codegen for service-style Rust backends.
Quick-reference recipe card - copy-paste ready.
use sea_orm::{entity::*, query::*, Database};
let db = Database::connect(&database_url).await?;
let users = User::find()
.filter(user::Column::Active.eq(true))
.all(&db)
.await?;When to reach for this: You want async ORM ergonomics, relation helpers, and codegen without Diesel's sync-first model.
use sea_orm::{entity::prelude::*, Database, Set};
mod item {
use super::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "items")]
pub struct Model { #[sea_orm(primary_key)] pub id: i64, pub name: String }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
}
use item::{self, Entity as Item};
#[tokio::main]
async fn main() -> Result<(), sea_orm::DbErr> {
let db = Database::connect(&std::env::var("DATABASE_URL").unwrap()).await?;
let am = item::ActiveModel { name: Set("widget".to_string()), ..Default::default() };
let model = am.insert(&db).await?;
let all = Item::find().all(&db).await?;
println!("{:?} {:?}", model, all);
Ok(())
}What this demonstrates:
DeriveEntityModel codegen for table mapping.ActiveModel for inserts with Set fields.Entity::find() fluent query API.Database connection throughout.sea-orm-cli generates entities from migrations or database introspection.| Topic | SeaORM | sqlx |
|---|---|---|
| Queries | Builder + entities | SQL strings |
| Relations | Built-in | Manual joins |
| Dynamic filters | Strong support | Manual SQL building |
| Control | Less SQL visible | Full SQL |
// Pagination
let page = Item::find().paginate(&db, 50);
let models = page.fetch_page(0).await?;ActiveModel from APIs. Fix: Map to DTOs at boundary.load_related in loops. Fix: Eager find_with_related patterns.sea-orm-cli generate.| Alternative | Use When | Don't Use When |
|---|---|---|
| sqlx | SQL control, simpler stack | Rich relation API needed |
| Diesel | Compile-time query builder | Native async required |
tokio-postgres | Minimal abstraction | CRUD-heavy services |
PostgreSQL, MySQL, SQLite via sqlx backends.
db.begin().await? returns transaction handle for entity ops.
Global filter patterns on deleted_at column in entity queries.
Json column type with serde types.
In-memory SQLite Database::connect("sqlite::memory:").
Store DatabaseConnection in AppState - cheap clone.
Entity::insert_many(active_models).exec(&db).
sea-orm-migration crate with Rust migration files.
Profile generated SQL - ORM can emit suboptimal queries.
Heavy custom SQL, data warehouse queries, or tiny micro-CRUD with sqlx only.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026