System Architecture & Design
Relevant source files
The following files were used as context for generating this wiki page:
SeaORM is a relational Object-Relational Mapper (ORM) designed for building web services in Rust. Its architecture prioritizes developer experience by mirroring concepts from popular ecosystem ORMs (like those in Python or Node.js) while maintaining Rust's strict type safety and performance. The system supports various database backends including MySQL, PostgreSQL, and SQLite, and provides both asynchronous and synchronous execution models.
The core design philosophy revolves around an "Entity-First" workflow, where the database schema can be derived from Rust structs or vice versa via code generation tools. SeaORM 2.0 introduces significant architectural changes, specifically a "Dense" entity format and a unified #[sea_orm::model] macro that simplifies relationship definitions.
Sources: src/lib.rs:25-45, CLAUDE.md:1-10
Core Components
The SeaORM architecture is composed of several specialized modules that handle everything from query construction to code generation.
| Component | Description |
|---|---|
| Entity | Defines the mapping between Rust structs and database tables. |
| ActiveModel | A stateful wrapper around models used for CRUD operations (Insert, Update, Delete). |
| Query | Provides a fluent API for building complex SQL queries including filters and joins. |
| Executor | Handles the actual execution of SQL statements against the database connection. |
| Codegen | A toolset (sea-orm-cli) for generating Rust entity files from existing database schemas. |
| Schema | Manages database migrations and schema synchronization. |
Sources: src/lib.rs:480-500, README.md:25-50
Data Flow & Execution Model
SeaORM supports two primary execution patterns: Asynchronous (via sea-orm) and Synchronous (via sea-orm-sync). The asynchronous model is the default for high-performance web services, while the synchronous model is optimized for CLI tools or lightweight SQLite applications.
Query Execution Flow
When a user executes a query, the system follows a path from high-level Entity methods down to raw SQL execution.
Entity & Model Design
In SeaORM 2.0, the entity definition has been consolidated. Relationships are now defined directly on the Model struct using attributes, replacing the older pattern of separate enums and traits.
Relation Types
The system supports several relationship types defined via macros:
- HasOne: 1-to-1 relationship.
- HasMany: 1-to-N relationship.
- BelongsTo: Defines the foreign key mapping back to a parent.
- Many-to-Many: Uses a junction table via the
viaattribute.
Sources: CLAUDE.md:14-50, src/lib.rs:100-120
Smart Entity Loader
The Entity Loader is a high-level API that optimizes relational queries to solve the N+1 problem. It intelligently uses SQL JOIN for 1-to-1 relations and a specialized "data loader" (separate queries with IN clauses) for 1-to-N and M-to-N relations.
Example Logic:
- Join: Used for
user -> profile(1-1). - Data Loader: Used for
user -> posts(1-N). - Nested Data Loader: Used for
posts -> tags(M-N).
Sources: src/lib.rs:145-170
Code Generation Architecture
The sea-orm-codegen crate provides the logic for the sea-orm-cli. It supports multiple output formats to suit different project styles.
Output Formats
- Compact: Balanced layout with related traits in one file.
- Expanded: Highly verbose, splitting traits into explicit blocks.
- Dense: (New in 2.0) Minimizes boilerplate by using the
#[sea_orm::model]macro. - Frontend: Specialized output for frontend-facing models.
Persistence with ActiveModel
ActiveModel is the primary interface for mutating data. It tracks changes to fields (using Set, Unchanged, or NotSet states) to ensure that UPDATE queries only modify necessary columns, avoiding race conditions.
Nested Persistence
The system supports persisting entire object graphs. A single save or insert call on a builder can trigger a sequence of operations where SeaORM resolves dependencies (e.g., inserting a User first to get an ID before inserting their Profile).
Sources: src/lib.rs:200-220, CLAUDE.md:85-105
Conclusion
SeaORM's architecture provides a robust, type-safe abstraction over SQL while remaining flexible enough for complex relational modeling. By offering both an "Entity-First" and "Schema-First" workflow, along with smart loading strategies and multi-backend support, it serves as a comprehensive toolkit for Rust database development.
Sources: src/lib.rs:500-520