Insert the model if primary key is `NotSet`, update otherwise. Only works if the entity has auto increment primary key.
(self, db: &'a C)
| 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 | /// |