SeaORM Documentation
repository·master·Indexed 27 days ago
https://github.com/seaql/sea-ormAn async and dynamic ORM for Rust (version 2.0.0) supporting advanced relations, nested persistence, and both schema-first and entity-first workflows. Features include the sea-orm-cli for entity generation, the raw_sql! macro for ergonomic parameter binding, and sea-orm-sync for synchronous usage in CLI programs. Supports complex queries via Partial Models, ActiveModel for data persistence, and integration with Rocket via sea-orm-rocket.
What's inside SeaORM
- SeaORM is a powerful and dynamic relational ORM for Rust, designed to help build web services with a developer experience similar to popular ORMs in Ruby, Python, and Node.js. It supports advanced relationship modeling (one-to-one, one-to-many, many-to-many, and self-referencing), built-in filtering, pagination, and nested queries. It is production-ready and widely used in both startups and large enterprises.
Use SeaORM synchronously with sea-orm-sync
masterIf you are building lightweight CLI programs (e.g., using SQLite) and do not want to require an async runtime, you can use thesea-orm-synccrate. It provides the full SeaORM API in a synchronous manner.SeaORM Pro: Professional Admin Panel
masterSeaORM Pro is an admin panel solution built with React and GraphQL. It provides full CRUD operations and allows for UI customization via TOML configuration. It also supports Role-Based Access Control (RBAC).Synchronous SQLite support via `sea-orm-sync`
masterA new crate,sea-orm-sync, is available for synchronous SQLite operations usingrusqlite. It provides support for streams and transactions, and integrates withsea-schema-sync.Seaography: Build GraphQL APIs from SeaORM entities
masterSeaography is a framework built on top of SeaORM that allows you to quickly build GraphQL resolvers. It enables you to start a fully functional GraphQL server from your SeaORM entities with support for filtering, pagination, relationship queries, and mutations.Set up DATABASE_URL for compilation
masterThis project uses theenv!()macro to connect to the database. You must set theDATABASE_URLenvironment variable at compile time for the project to compile successfully.Upgrade to SeaORM 2.0.0-rc.39 dependencies
masterSeaORM 2.0.0-rc.39 introduces breaking dependency changes by promoting core dependencies to new major/minor release lines. If upgrading from rc.38, be aware of the following version shifts:
sea-queryis now~1.0.0sea-query-sqlxis now0.9.0sea-schemaandsea-schema-syncare now0.18.0sqlxandsqlx-coreare now0.9.0
Note that runtime and driver feature wiring has been updated to accommodate SQLx 0.9's split runtime and TLS feature names.
Query models and handle relations in SeaORM
masterSeaORM provides several ways to query data, including finding all records, filtering, and handling relationships (one-to-one, one-to-many, and many-to-many).
Basic Queries
- Find all:
Entity::find().all(db) - Filter: Use
.filter(Entity::COLUMN.name.contains("value")) - Find by ID:
Entity::find_by_id(id).one(db)
Relationship Loading
- Lazy Loading: Use
model.find_related(RelatedEntity).one(db)to fetch related models on demand. - Eager Loading (One-to-One): Use
Entity::find().find_also_related(RelatedEntity).all(db)to fetch the main model and an optional related model in one query. - Eager Loading (One-to-Many/Many-to-Many): Use
Entity::find().find_with_related(RelatedEntity).all(db)to fetch the main model and a collection of related models. For many-to-many, this performs two joins and automatically merges rows by the left-side entity.
// Find all let cakes: Vec<cake::Model> = Cake::find().all(db).await?; // Filter let chocolate: Vec<cake::Model> = Cake::find() .filter(Cake::COLUMN.name.contains("chocolate")) .all(db) .await?; // Find by ID let cheese: Option<cake::Model> = Cake::find_by_id(1).one(db).await?; // Lazy loading related models let fruit: Option<fruit::Model> = cheese.find_related(Fruit).one(db).await?; // Eager loading (One-to-One) let cake_with_fruit: Vec<(cake::Model, Option<fruit::Model>)> = Cake::find().find_also_related(Fruit).all(db).await?; // Eager loading (One-to-Many/Many-to-Many) let cake_with_fillings: Vec<(cake::Model, Vec<filling::Model>)> = Cake::find() .find_with_related(Filling) .all(db) .await?;- Find all:
Configure SeaORM Decimal support
masterSeaORM supports two Rust decimal libraries for handling precise numeric values. You must enable the corresponding feature in your
Cargo.tomlto use them.rust_decimal(feature:with-rust_decimal):- Uses
rust_decimal::Decimal. - Precision: 28-29 significant digits.
- Best for: Most business and financial applications.
- Uses
bigdecimal(feature:with-bigdecimal):- Uses
bigdecimal::BigDecimal. - Precision: Arbitrary (limited by memory).
- Best for: Scientific computing or requirements exceeding 28 digits.
- Uses
Showcase your SeaORM application
masterIf you have built an application using SeaORM and wish to showcase it in the community list, you can do so by opening a Pull Request (PR) to the repository.Workaround for underscore field names in SeaORM
masterWhen dealing with database fields that use underscores, you may encounter issues where SeaORM does not map them correctly to your entity fields. This repository demonstrates a workaround located insrc/entity/underscores_workaround.rsto ensure queries use the correct field names.Enable tracing spans for database operations
masterSeaORM supports distributed tracing for database operations and transactions. To use this, enable thetracing-spansfeature in yourCargo.toml. Once enabled, database operations will emit tracing spans using theObservabilityHookand thewith_db_span!macro.