Derive Macro DerivePartialModel

#[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

For more complete examples, please refer to https://github.com/SeaQL/sea-orm/blob/master/tests/partial_model_tests.rs

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[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 specifying the entity can be skipped.

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,
}

Since SeaORM 1.1.7, DerivePartialModel can also assumes the function of FromQueryResult. This is necessary to support nested partial models.

use sea_orm::{DerivePartialModel, FromQueryResult};

#[derive(DerivePartialModel)]
#[sea_orm(entity = "cake::Entity", from_query_result)]
struct Cake {
    id: i32,
    name: String,
    #[sea_orm(nested)]
    bakery: Option<Bakery>,
    #[sea_orm(skip)]
    ignore: String,
}

#[derive(FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "bakery::Entity")]
struct Bakery {
    id: i32,
    #[sea_orm(from_col = "Name")]
    title: String,
}

// In addition, there's an `alias` attribute to select the columns from an alias:

#[derive(DerivePartialModel)]
#[sea_orm(entity = "bakery::Entity", alias = "factory", from_query_result)]
struct Factory {
    id: i32,
    #[sea_orm(from_col = "name")]
    plant: String,
}

#[derive(DerivePartialModel)]
#[sea_orm(entity = "cake::Entity", from_query_result)]
struct CakeFactory {
    id: i32,
    name: String,
    #[sea_orm(nested)]
    bakery: Option<Factory>,
}
let cake: CakeFactory = cake::Entity::find()
    .join_as(
        JoinType::LeftJoin,
        cake::Relation::Bakery.def(),
        Alias::new("factory"),
    )
    .order_by_asc(cake::Column::Id)
    .into_partial_model()
    .one(&db)
    .await
    .unwrap()
    .unwrap()

SELECT
    "cake"."id" AS "id", "cake"."name" AS "name",
    "factory"."id" AS "bakery_id", "factory"."name" AS "bakery_plant"
FROM "cake"
LEFT JOIN "bakery" AS "factory" ON "cake"."bakery_id" = "factory"."id"
LIMIT 1

A field cannot have attributes from_col, from_expr or nested 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,
}