Entity Generation
Relevant source files
The following files were used as context for generating this wiki page:
Entity Generation in SeaORM Pro is the process of mapping database schemas into Rust source code to provide a type-safe interface for database operations. This system utilizes sea-orm-cli to automatically generate model definitions, relations, and active model behaviors from an existing database. The generated entities serve as the foundational layer for both the Admin Panel and the GraphQL API, ensuring that the application remains synchronized with the underlying database structure.
Sources: README.md:100-110, examples/axum_example/entity/src/post.rs:1
Generation Workflow
The workflow for entity generation typically involves defining or modifying a database schema and then using the SeaORM command-line interface to synchronize the Rust models.
Automatic Code Generation
The primary tool for this process is sea-orm-cli. When executed with the generate entity command, it inspects the database defined in the DATABASE_URL environment variable and produces Rust files. In SeaORM Pro, the --seaography flag is often included to generate additional metadata required for GraphQL integration.
The diagram above illustrates the flow from a database instance to the generated Rust source files.
Sources: README.md:104-106, src/models/mod.rs:1
Manual Integration
After the CLI generates the files, manual registration is required for custom entities or those excluded by the generator. For instance, the user model is often added back manually to the mod.rs file to ensure it is included in the project's module tree.
Sources: README.md:108-115, src/models/mod.rs:18-20
Entity Structure
Generated entities follow a specific structure comprised of the Model, Relation, and ActiveModel segments.
Model Definition
The Model struct represents a row in the database table. It utilizes derive macros such as DeriveEntityModel and attributes like #[sea_orm(table_name = "...")] to map Rust fields to database columns.
| Component | Description | Attribute/Trait |
|---|---|---|
| Primary Key | Identifies the unique record. | #[sea_orm(primary_key)] |
| Unique Constraints | Ensures field uniqueness. | #[sea_orm(unique)] |
| Column Type | Overrides default type mapping. | #[sea_orm(column_type = "Text")] |
| Serialization | Handles JSON conversion. | #[derive(Serialize, Deserialize)] |
Sources: examples/axum_example/entity/src/post.rs:7-17, src/models/customer.rs:7-25
Relationships and Navigation
Relationships are defined via the Relation enum. This allows the ORM to perform joins and navigate between related entities like Customer and Address.
This ER diagram represents the relationships generated for the Customer and Address modules.
Sources: src/models/customer.rs:30-36, src/models/customer_address.rs:20-35
Seaography and GraphQL Integration
SeaORM Pro leverages seaography to bridge the gap between generated entities and a dynamic GraphQL schema.
Entity Registration
Entities generated with the --seaography flag are registered in src/models/mod.rs. This registration allows the GraphQL Builder to automatically discover and expose these entities as queryable types.
seaography::register_entity_modules!([
address,
customer,
customer_address,
product,
// ... other entities
]);
Sources: src/models/mod.rs:18-28, src/graphql/query_root.rs:32-34
Field Annotations
Generated code can include #[seaography(ignore)] attributes to prevent sensitive fields, such as password or api_key, from being exposed through the GraphQL API.
Sources: src/models/user.rs:15-18
Summary of Generated Components
| File | Purpose | Source Reference |
|---|---|---|
mod.rs | Main module entry point and entity registration. | src/models/mod.rs |
prelude.rs | Convenient re-exports of all Entity types. | src/models/prelude.rs |
[entity].rs | Specific definitions for a table (Model, Relation). | src/models/product.rs |
Sources: src/models/mod.rs:1-15, src/models/prelude.rs:3-15
Entity Generation serves as the bridge between the relational database and the Rust application logic. By automating the creation of structs and relationships, the system ensures type safety across the database access layer and provides the necessary metadata for the Admin Panel and GraphQL interfaces to function out-of-the-box.