SeaORM Documentation

repository·master·Indexed 27 days ago

https://github.com/seaql/sea-orm

An 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.

Tokens
34.2K
Snippets
102
Records
187
Agent score
92%

What's inside SeaORM

  1. Overview of SeaORM

    master
    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.
  2. Use SeaORM synchronously with sea-orm-sync

    master
    If you are building lightweight CLI programs (e.g., using SQLite) and do not want to require an async runtime, you can use the sea-orm-sync crate. It provides the full SeaORM API in a synchronous manner.
  3. SeaORM Pro: Professional Admin Panel

    master
    SeaORM 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).
  4. Seaography: Build GraphQL APIs from SeaORM entities

    master
    Seaography 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.
  5. Upgrade to SeaORM 2.0.0-rc.39 dependencies

    master

    SeaORM 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-query is now ~1.0.0
    • sea-query-sqlx is now 0.9.0
    • sea-schema and sea-schema-sync are now 0.18.0
    • sqlx and sqlx-core are now 0.9.0

    Note that runtime and driver feature wiring has been updated to accommodate SQLx 0.9's split runtime and TLS feature names.

  6. Query models and handle relations in SeaORM

    master

    SeaORM 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?;
  7. Configure SeaORM Decimal support

    master

    SeaORM supports two Rust decimal libraries for handling precise numeric values. You must enable the corresponding feature in your Cargo.toml to use them.

    1. rust_decimal (feature: with-rust_decimal):

      • Uses rust_decimal::Decimal.
      • Precision: 28-29 significant digits.
      • Best for: Most business and financial applications.
    2. bigdecimal (feature: with-bigdecimal):

      • Uses bigdecimal::BigDecimal.
      • Precision: Arbitrary (limited by memory).
      • Best for: Scientific computing or requirements exceeding 28 digits.
  8. Workaround for underscore field names in SeaORM

    master
    When 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 in src/entity/underscores_workaround.rs to ensure queries use the correct field names.
  9. Enable tracing spans for database operations

    master
    SeaORM supports distributed tracing for database operations and transactions. To use this, enable the tracing-spans feature in your Cargo.toml. Once enabled, database operations will emit tracing spans using the ObservabilityHook and the with_db_span! macro.