+2
日增长
9,970
stars
749
forks
38
watchers
1,206
prs
1,094
issues
106
releases
README
一个强大且动态的 Rust ORM
请给我们一个 ⭐ 以支持我们!
🐚 SeaORM
SeaORM 是一个关系型 ORM,帮助你在 Rust 中构建 Web 服务,同时提供动态语言的使用体验。
高级关系
以高层次、概念化的方式建模复杂关系:一对一、一对多、多对多,甚至自引用。
熟悉的概念
受 Ruby、Python 和 Node.js 生态系统中流行 ORM 的启发,SeaORM 提供的开发体验让你感觉似曾相识。
功能丰富
SeaORM 是一个功能齐全的 ORM,内置过滤、分页和嵌套查询,加速构建 REST、GraphQL 和 gRPC API。
生产就绪
SeaORM 周下载量超过 25 万次,已被全球的初创企业和大型企业采用,适用于生产环境。
快速开始
加入我们的 Discord 服务器,与其他成员交流!
+ 中文文档
集成示例:
+ Actix 示例
+ Axum 示例
+ GraphQL 示例
+ jsonrpsee 示例
+ Loco 示例 / Loco REST 入门
+ Poem 示例
+ Rocket 示例 / Rocket OpenAPI 示例
+ Salvo 示例
+ Tonic 示例
+ Seaography 示例 (Bakery) / Seaography 示例 (Sakila)
如果你想要一个简洁的单文件示例来展示 SeaORM 的精华,可以试试:
+ 快速入门
让我们快速了解一下 SeaORM 的独特功能。
灵活的实体格式
你不需要手写这些!实体文件可以使用 sea-orm-cli 从现有数据库生成,
以下代码通过 --entity-format dense 生成 (2.0 新增)。
rust
mod user {
use sea_orm::entity::prelude::;
[sea_orm::model]
[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
[sea_orm(table_name = "user")]
pub struct Model {
[sea_orm(primary_key)]
pub id: i32,
pub name: String,
[sea_orm(unique)]
pub email: String,
[sea_orm(has_one)]
pub profile: HasOne,
[sea_orm(has_many)]
pub posts: HasMany,
}
}
mod post {
use sea_orm::entity::prelude::;
[sea_orm::model]
[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
[sea_orm(table_name = "post")]
pub struct Model {
[sea_orm(primary_key)]
pub id: i32,
pub user_id: i32,
pub title: String,
[sea_orm(belongs_to, from = "user_id", to = "id")]
pub author: BelongsTo,
[sea_orm(has_many, via = "post_tag")] // 多对多关系,使用中间表
pub tags: HasMany,
}
}
智能实体加载器
实体加载器智能地对一对一关系使用 join,对一对多关系使用 data loader,
即使在执行嵌套查询时也能消除 N+1 问题。
rust
// 加载路径:
// user - profile
// user - post
// post - post_tag - tag
let smart_user = user::Entity::load()
.filter_by_id(42) // 等价于 .filter(user::COLUMN.id.eq(42))
.with(profile::Entity) // 一对一使用 join
.with((post::Entity, tag::Entity)) // 一对多使用 data loader
.one(db)
.await?
.unwrap();
// 底层执行 3 个查询:
// 1. SELECT FROM user JOIN profile WHERE id = $
// 2. SELECT FROM post WHERE user_id IN (..)
// 3. SELECT FROM tag JOIN post_tag WHERE post_id IN (..)
smart_user
== user::ModelEx {
id: 42,
name: "Bob".into(),
email: "[email protected]".into(),
profile: HasOne::loaded(Some(profile::ModelEx {
picture: "image.jpg".into(),
})),
posts: HasMany::Loaded(vec![post::ModelEx {
title: "Nice weather".into(),
tags: HasMany::Loaded(vec![tag::ModelEx {
tag: "sunny".into(),
}]),
}]),
};
ActiveModel:简化嵌套持久化
通过流畅的 builder API,在单次操作中持久化整个对象图:用户、个人资料(一对一)、
帖子(一对多)和标签(多对多)。SeaORM 自动确定依赖关系,
以正确的顺序插入或删除对象。
rust
// 创建上面展示的嵌套对象:
let user = user::ActiveModel::builder()
.set_name("Bob")
.set_email("[email protected]")
.set_profile(profile::ActiveModel::builder().set_picture("image.Top issues
#220[Feature Request] Soft Delete18 comments · 2021-10-03#924Uuid/Datetime/etc. types (instead of String) for generated models with sqlite databases14 comments · 2022-07-29#1123[PIP] Event stream12 comments · 2022-10-16#2423Returning model with `enum` field with `find_by_statement`10 comments · 2024-11-20#591[ADD]: Pre-commit hook to keep the project clean10 comments · 2022-03-10#3183Nested ActiveModelEx::save() fails to insert a new has_many child with a composite, non-auto-increment primary key9 comments · 2026-08-22#3034Postgres tsvector/tsquery support9 comments · 2026-04-02#2749Feature Request: Support for async closures9 comments · 2025-10-12
Discussions
#3262camelCase for custom query and mutation names1 comments · 2026-11-11#3260Specify rs_type on generated enum1 comments · 2026-11-10#3239How to mock Entity Loader-type queries? (Or, examples on how to mock complex relationships?)2 comments · 2026-10-23#3230From `DeriveIntoActiveModel` to a Rust MapStruct: should SeaORM add `DeriveIntoVo` or incubate a dedicated mapper?2 comments · 2026-10-10#3223Invite more contributors and maintainers to help SesQL grow1 comments · 2026-10-05#3190manage migration files in entity firs aproach, verify them, apply them either using seaorm migration files or flyway1 comments · 2026-09-01#3181Updating ActiveModel with deeply nested ActiveModel structure after the fact2 comments · 2026-08-21#3170Why is `Eq` skipped in `ModelEx`?1 comments · 2026-08-13
Latest release
2.0.4 2.0.4 2026-09-27SeaORM 2.0.4
(since 2.0.3)
Enhancements
Add select_except to select all columns except the given ones https://github.com/SeaQL/sea-orm/pull/3211
select_except provides the inverse of select_only / column——selecting all columns except the specified ones.
These methods replace the current selection, so the last one called takes effect.
rust
[derive(DerivePartialModel)]
[sea_orm(entity = "post::Entity")]
struct PostPreview {
id: i32,
title: String,
}
let posts = post::Entity::find()
.select_except([post::Column::Body])
.into_model::()
.all(db