sea_orm_macros

Derive Macro DerivePartialModel

Source
#[derive(DerivePartialModel)]
{
    // Attributes available to this derive:
    #[sea_orm]
}
Expand description

The DerivePartialModel derive macro will implement sea_orm::PartialModelTrait for simplify partial model queries.

§Usage

use sea_orm::{entity::prelude::*, sea_query::Expr, DerivePartialModel, FromQueryResult};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "posts")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub title: String,
    #[sea_orm(column_type = "Text")]
    pub text: String,
}

#[derive(Debug, FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "Entity")]
struct SelectResult {
    title: String,
    #[sea_orm(from_col = "text")]
    content: String,
    #[sea_orm(from_expr = "Expr::val(1).add(1)")]
    sum: i32,
}

If all fields in the partial model is from_expr, the entity can be ignore.

use sea_orm::{entity::prelude::*, sea_query::Expr, DerivePartialModel, FromQueryResult};

#[derive(Debug, FromQueryResult, DerivePartialModel)]
struct SelectResult {
    #[sea_orm(from_expr = "Expr::val(1).add(1)")]
    sum: i32,
}

A field cannot have attributes from_col and from_expr at the same time. Or, it will result in a compile error.

use sea_orm::{entity::prelude::*, FromQueryResult, DerivePartialModel, sea_query::Expr};

#[derive(Debug, FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "Entity")]
struct SelectResult {
    #[sea_orm(from_expr = "Expr::val(1).add(1)", from_col = "foo")]
    sum: i32
}