pub trait ActiveModelTrait: Clone + Debug {
type Entity: EntityTrait;
Show 20 methods
// Required methods
fn take(
&mut self,
c: <Self::Entity as EntityTrait>::Column,
) -> ActiveValue<Value>;
fn get(
&self,
c: <Self::Entity as EntityTrait>::Column,
) -> ActiveValue<Value>;
fn set_if_not_equals(
&mut self,
c: <Self::Entity as EntityTrait>::Column,
v: Value,
);
fn try_set(
&mut self,
c: <Self::Entity as EntityTrait>::Column,
v: Value,
) -> Result<(), DbErr>;
fn not_set(&mut self, c: <Self::Entity as EntityTrait>::Column);
fn is_not_set(&self, c: <Self::Entity as EntityTrait>::Column) -> bool;
fn default() -> Self;
fn default_values() -> Self;
fn reset(&mut self, c: <Self::Entity as EntityTrait>::Column);
// Provided methods
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value) { ... }
fn reset_all(self) -> Self { ... }
fn get_primary_key_value(&self) -> Option<ValueTuple> { ... }
fn insert<'a, C>(
self,
db: &'a C,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>
where <Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait { ... }
fn update<'a, C>(
self,
db: &'a C,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>
where <Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait { ... }
fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
where <Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait { ... }
fn delete<'a, C>(self, db: &'a C) -> Result<DeleteResult, DbErr>
where Self: ActiveModelBehavior,
C: ConnectionTrait { ... }
fn set_from_json(&mut self, json: Value) -> Result<(), DbErr>
where Self: TryIntoModel<<Self::Entity as EntityTrait>::Model>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self> + Deserialize<'de> + Serialize { ... }
fn from_json(json: Value) -> Result<Self, DbErr>
where Self: TryIntoModel<<Self::Entity as EntityTrait>::Model>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self> + Deserialize<'de> + Serialize { ... }
fn is_changed(&self) -> bool { ... }
fn find_related<R>(&self, _: R) -> Select<R>
where R: EntityTrait,
Self::Entity: Related<R> { ... }
}Expand description
ActiveModel is a type for constructing INSERT and UPDATE statements for a particular table.
Like [Model][ModelTrait], it represents a database record and each field represents a column.
But unlike [Model][ModelTrait], it also stores additional state for every field, and fields are not guaranteed to have a value.
This allows you to:
- omit columns from the query,
- know which columns have changed after editing a record.
Required Associated Types§
Sourcetype Entity: EntityTrait
type Entity: EntityTrait
The Entity this ActiveModel belongs to
Required Methods§
Sourcefn take(
&mut self,
c: <Self::Entity as EntityTrait>::Column,
) -> ActiveValue<Value>
fn take( &mut self, c: <Self::Entity as EntityTrait>::Column, ) -> ActiveValue<Value>
Get a mutable ActiveValue from an ActiveModel
Sourcefn get(&self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>
fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>
Get a immutable ActiveValue from an ActiveModel
Sourcefn set_if_not_equals(
&mut self,
c: <Self::Entity as EntityTrait>::Column,
v: Value,
)
fn set_if_not_equals( &mut self, c: <Self::Entity as EntityTrait>::Column, v: Value, )
Set the Value of a ActiveModel field if value is different, panic if failed
Sourcefn try_set(
&mut self,
c: <Self::Entity as EntityTrait>::Column,
v: Value,
) -> Result<(), DbErr>
fn try_set( &mut self, c: <Self::Entity as EntityTrait>::Column, v: Value, ) -> Result<(), DbErr>
Set the Value of a ActiveModel field, return error if failed
Sourcefn not_set(&mut self, c: <Self::Entity as EntityTrait>::Column)
fn not_set(&mut self, c: <Self::Entity as EntityTrait>::Column)
Set the state of an ActiveValue to the not set state
Sourcefn is_not_set(&self, c: <Self::Entity as EntityTrait>::Column) -> bool
fn is_not_set(&self, c: <Self::Entity as EntityTrait>::Column) -> bool
Check the state of a ActiveValue
Sourcefn default_values() -> Self
fn default_values() -> Self
Create an ActiveModel with all fields to Set(default_value) if Default is implemented, NotSet otherwise
Sourcefn reset(&mut self, c: <Self::Entity as EntityTrait>::Column)
fn reset(&mut self, c: <Self::Entity as EntityTrait>::Column)
Reset the value from ActiveValue::Unchanged to ActiveValue::Set, leaving ActiveValue::NotSet untouched.
Provided Methods§
Sourcefn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value)
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value)
Set the Value of a ActiveModel field, panic if failed
Sourcefn reset_all(self) -> Self
fn reset_all(self) -> Self
Reset all values from ActiveValue::Unchanged to ActiveValue::Set, leaving ActiveValue::NotSet untouched.
Sourcefn get_primary_key_value(&self) -> Option<ValueTuple>
fn get_primary_key_value(&self) -> Option<ValueTuple>
Get the primary key of the ActiveModel, only if it’s fully specified.
Sourcefn insert<'a, C>(
self,
db: &'a C,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait,
fn insert<'a, C>(
self,
db: &'a C,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait,
Perform an INSERT operation on the ActiveModel
§Example (Postgres)
use sea_orm::{entity::*, query::*, tests_cfg::cake};
let apple = cake::ActiveModel {
name: Set("Apple Pie".to_owned()),
..Default::default()
};
assert_eq!(
apple.insert(&db)?,
cake::Model {
id: 15,
name: "Apple Pie".to_owned(),
}
);
assert_eq!(
db.into_transaction_log(),
[Transaction::from_sql_and_values(
DbBackend::Postgres,
r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id", "name""#,
["Apple Pie".into()]
)]
);§Example (MySQL)
use sea_orm::{entity::*, query::*, tests_cfg::cake};
let apple = cake::ActiveModel {
name: Set("Apple Pie".to_owned()),
..Default::default()
};
assert_eq!(
apple.insert(&db)?,
cake::Model {
id: 15,
name: "Apple Pie".to_owned(),
}
);
assert_eq!(
db.into_transaction_log(),
[
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"INSERT INTO `cake` (`name`) VALUES (?)"#,
["Apple Pie".into()]
),
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = ? LIMIT ?"#,
[15.into(), 1u64.into()]
)
]
);Sourcefn update<'a, C>(
self,
db: &'a C,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait,
fn update<'a, C>(
self,
db: &'a C,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait,
Perform the UPDATE operation on an ActiveModel
§Example (Postgres)
use sea_orm::{entity::*, query::*, tests_cfg::fruit};
let orange = fruit::ActiveModel {
id: Set(1),
name: Set("Orange".to_owned()),
..Default::default()
};
assert_eq!(
orange.update(&db)?,
fruit::Model {
id: 1,
name: "Orange".to_owned(),
cake_id: None,
}
);
assert_eq!(
db.into_transaction_log(),
[Transaction::from_sql_and_values(
DbBackend::Postgres,
r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2 RETURNING "id", "name", "cake_id""#,
["Orange".into(), 1i32.into()]
)]);§Example (MySQL)
use sea_orm::{entity::*, query::*, tests_cfg::fruit};
let orange = fruit::ActiveModel {
id: Set(1),
name: Set("Orange".to_owned()),
..Default::default()
};
assert_eq!(
orange.update(&db)?,
fruit::Model {
id: 1,
name: "Orange".to_owned(),
cake_id: None,
}
);
assert_eq!(
db.into_transaction_log(),
[
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"UPDATE `fruit` SET `name` = ? WHERE `fruit`.`id` = ?"#,
["Orange".into(), 1i32.into()]
),
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = ? LIMIT ?"#,
[1i32.into(), 1u64.into()]
)]);Sourcefn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait,
fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior,
C: ConnectionTrait,
Insert the model if primary key is NotSet, update otherwise.
Only works if the entity has auto increment primary key.
Sourcefn delete<'a, C>(self, db: &'a C) -> Result<DeleteResult, DbErr>where
Self: ActiveModelBehavior,
C: ConnectionTrait,
fn delete<'a, C>(self, db: &'a C) -> Result<DeleteResult, DbErr>where
Self: ActiveModelBehavior,
C: ConnectionTrait,
Delete an active model by its primary key
§Example
use sea_orm::{entity::*, query::*, tests_cfg::fruit};
let orange = fruit::ActiveModel {
id: Set(3),
..Default::default()
};
let delete_result = orange.delete(&db)?;
assert_eq!(delete_result.rows_affected, 1);
assert_eq!(
db.into_transaction_log(),
[Transaction::from_sql_and_values(
DbBackend::Postgres,
r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#,
[3i32.into()]
)]
);Sourcefn set_from_json(&mut self, json: Value) -> Result<(), DbErr>where
Self: TryIntoModel<<Self::Entity as EntityTrait>::Model>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self> + Deserialize<'de> + Serialize,
Available on crate feature with-json only.
fn set_from_json(&mut self, json: Value) -> Result<(), DbErr>where
Self: TryIntoModel<<Self::Entity as EntityTrait>::Model>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self> + Deserialize<'de> + Serialize,
with-json only.Set the corresponding attributes in the ActiveModel from a JSON value
Note that this method will not alter the primary key values in ActiveModel.
Sourcefn from_json(json: Value) -> Result<Self, DbErr>where
Self: TryIntoModel<<Self::Entity as EntityTrait>::Model>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self> + Deserialize<'de> + Serialize,
Available on crate feature with-json only.
fn from_json(json: Value) -> Result<Self, DbErr>where
Self: TryIntoModel<<Self::Entity as EntityTrait>::Model>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self> + Deserialize<'de> + Serialize,
with-json only.Create ActiveModel from a JSON value
Sourcefn is_changed(&self) -> bool
fn is_changed(&self) -> bool
Return true if any attribute of ActiveModel is Set
Find related Models belonging to self
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
Source§impl ActiveModelTrait for sea_orm::rbac::entity::permission::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::permission::ActiveModel
rbac only.Source§impl ActiveModelTrait for sea_orm::rbac::entity::resource::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::resource::ActiveModel
rbac only.Source§impl ActiveModelTrait for sea_orm::rbac::entity::role::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::role::ActiveModel
rbac only.Source§impl ActiveModelTrait for sea_orm::rbac::entity::role_hierarchy::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::role_hierarchy::ActiveModel
rbac only.Source§impl ActiveModelTrait for sea_orm::rbac::entity::role_permission::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::role_permission::ActiveModel
rbac only.Source§impl ActiveModelTrait for sea_orm::rbac::entity::user_override::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::user_override::ActiveModel
rbac only.Source§impl ActiveModelTrait for sea_orm::rbac::entity::user_role::ActiveModel
Available on crate feature rbac only.
impl ActiveModelTrait for sea_orm::rbac::entity::user_role::ActiveModel
rbac only.