Skip to main content

IntoActiveModel

Trait IntoActiveModel 

Source
pub trait IntoActiveModel<A>{
    // Required method
    fn into_active_model(self) -> A;
}
Expand description

A Trait for any type that can be converted into an ActiveModel, can be derived using DeriveIntoActiveModel.

§Derive Macro Example

use sea_orm::DeriveIntoActiveModel;
mod fruit {
    use sea_orm::entity::prelude::*;
    #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
    #[sea_orm(table_name = "fruit")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i32,
        pub name: String,
        pub cake_id: Option<i32>,
    }
    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {}
    impl ActiveModelBehavior for ActiveModel {}
}

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = "fruit::ActiveModel")]
struct NewFruit {
    name: String,
    // `id` and `cake_id` are omitted - they become `NotSet`
}

§set(...) - always set absent ActiveModel fields

use sea_orm::DeriveIntoActiveModel;

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = "fruit::ActiveModel", set(cake_id = "None"))]
struct NewFruit {
    name: String,
    // `cake_id` is not on the struct, but will always be `Set(None)`
}

§default = "expr" - fallback for Option<T> struct fields

use sea_orm::DeriveIntoActiveModel;

#[derive(DeriveIntoActiveModel)]
#[sea_orm(active_model = "fruit::ActiveModel")]
struct UpdateFruit {
    /// `Some("Apple")` -> `Set("Apple")`, `None` ->`Set("Unnamed")`
    #[sea_orm(default = "String::from(\"Unnamed\")")]
    name: Option<String>,
}

Required Methods§

Source

fn into_active_model(self) -> A

Method to call to perform the conversion

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl IntoActiveModel<ActiveModel> for Model

Implementors§

Source§

impl<A> IntoActiveModel<A> for A