MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / save

Method save

src/entity/active_model.rs:409–429  ·  view source on GitHub ↗

Insert the model if primary key is `NotSet`, update otherwise. Only works if the entity has auto increment primary key.

(self, db: &'a C)

Source from the content-addressed store, hash-verified

407 /// Insert the model if primary key is `NotSet`, update otherwise.
408 /// Only works if the entity has auto increment primary key.
409 async fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
410 where
411 <Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
412 Self: ActiveModelBehavior + 'a,
413 C: ConnectionTrait,
414 {
415 let mut is_update = true;
416 for key in <Self::Entity as EntityTrait>::PrimaryKey::iter() {
417 let col = key.into_column();
418 if self.is_not_set(col) {
419 is_update = false;
420 break;
421 }
422 }
423 let res = if !is_update {
424 self.insert(db).await
425 } else {
426 self.update(db).await
427 }?;
428 Ok(res.into_active_model())
429 }
430
431 /// Delete an active model by its primary key
432 ///

Callers 15

exists_with_resultFunction · 0.80
exists_with_joinsFunction · 0.80
exists_with_orderingFunction · 0.80
exists_with_limit_offsetFunction · 0.80
insert_active_enumFunction · 0.80
insert_active_enum_childFunction · 0.80
insert_active_enum_vecFunction · 0.80
insert_teasFunction · 0.80

Calls 4

is_not_setMethod · 0.80
into_active_modelMethod · 0.80
insertMethod · 0.45
updateMethod · 0.45

Tested by 15

exists_with_resultFunction · 0.64
exists_with_joinsFunction · 0.64
exists_with_orderingFunction · 0.64
exists_with_limit_offsetFunction · 0.64
insert_active_enumFunction · 0.64
insert_active_enum_childFunction · 0.64
insert_active_enum_vecFunction · 0.64
insert_teasFunction · 0.64