pub trait ActiveModelTrait: Clone + Debug {
type Entity: EntityTrait;
Show 14 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(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
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 get_primary_key_value(&self) -> Option<ValueTuple> { ... }
fn insert<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn update<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn save<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn delete<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<DeleteResult, DbErr>> + Send + 'async_trait>>
where
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn set_from_json(&mut self, json: Value) -> Result<(), DbErr>
where
<<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: Deserialize<'de>,
{ ... }
fn from_json(json: Value) -> Result<Self, DbErr>
where
<<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: Deserialize<'de>,
{ ... }
fn is_changed(&self) -> bool { ... }
}
Expand description
A Trait for ActiveModel to perform Create, Update or Delete operation. The type must also implement the EntityTrait. See module level docs crate::entity for a full example
Required Associated Types
type Entity: EntityTrait
type Entity: EntityTrait
The Entity this ActiveModel belongs to
Required Methods
fn 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
fn 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
Set the Value into an ActiveModel
fn 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
fn 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
Provided Methods
fn get_primary_key_value(&self) -> Option<ValueTuple>
fn get_primary_key_value(&self) -> Option<ValueTuple>
Get the primary key of the ActiveModel
fn insert<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>> where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
fn insert<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>> where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
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).await?,
cake::Model {
id: 15,
name: "Apple Pie".to_owned(),
}
);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::from_sql_and_values(
DbBackend::Postgres,
r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id", "name""#,
vec!["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).await?,
cake::Model {
id: 15,
name: "Apple Pie".to_owned(),
}
);
assert_eq!(
db.into_transaction_log(),
vec![
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"INSERT INTO `cake` (`name`) VALUES (?)"#,
vec!["Apple Pie".into()]
),
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = ? LIMIT ?"#,
vec![15.into(), 1u64.into()]
)
]
);
fn update<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>> where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
fn update<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>> where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
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).await?,
fruit::Model {
id: 1,
name: "Orange".to_owned(),
cake_id: None,
}
);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::from_sql_and_values(
DbBackend::Postgres,
r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2 RETURNING "id", "name", "cake_id""#,
vec!["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).await?,
fruit::Model {
id: 1,
name: "Orange".to_owned(),
cake_id: None,
}
);
assert_eq!(
db.into_transaction_log(),
vec![
Transaction::from_sql_and_values(
DbBackend::MySql,
r#"UPDATE `fruit` SET `name` = ? WHERE `fruit`.`id` = ?"#,
vec!["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 ?"#,
vec![1i32.into(), 1u64.into()]
)]);
fn save<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>> where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
fn save<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>> where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
Insert the model if primary key is NotSet
, update otherwise.
Only works if the entity has auto increment primary key.
fn delete<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<DeleteResult, DbErr>> + Send + 'async_trait>> where
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
fn delete<'a, 'async_trait, C>(
self,
db: &'a C
) -> Pin<Box<dyn Future<Output = Result<DeleteResult, DbErr>> + Send + 'async_trait>> where
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait,
'a: 'async_trait,
C: 'async_trait,
Self: Send + 'async_trait,
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).await?;
assert_eq!(delete_result.rows_affected, 1);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::from_sql_and_values(
DbBackend::Postgres,
r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#,
vec![3i32.into()]
)]
);
fn set_from_json(&mut self, json: Value) -> Result<(), DbErr> where
<<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: Deserialize<'de>,
fn set_from_json(&mut self, json: Value) -> Result<(), DbErr> where
<<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: Deserialize<'de>,
Set the corresponding attributes in the ActiveModel from a JSON value
Note that this method will not alter the primary key values in ActiveModel.
fn from_json(json: Value) -> Result<Self, DbErr> where
<<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: Deserialize<'de>,
fn from_json(json: Value) -> Result<Self, DbErr> where
<<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: Deserialize<'de>,
Create ActiveModel from a JSON value
fn is_changed(&self) -> bool
fn is_changed(&self) -> bool
Returns true
if any columns were changed.