From 4604cefc62a94704903fe2825eb93411e4968246 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 Feb 2023 16:19:27 +0800 Subject: [PATCH] Added `ActiveValue::reset` to convert `Unchanged` into `Set` (SeaQL/sea-orm#1177) --- SeaORM/docs/05-basic-crud/03-insert.md | 14 ++++++++++++++ SeaORM/docs/05-basic-crud/04-update.md | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/SeaORM/docs/05-basic-crud/03-insert.md b/SeaORM/docs/05-basic-crud/03-insert.md index f370757aeee..6e2ae9d7fad 100644 --- a/SeaORM/docs/05-basic-crud/03-insert.md +++ b/SeaORM/docs/05-basic-crud/03-insert.md @@ -16,6 +16,20 @@ let _: ActiveValue = Set(10); let _: ActiveValue = NotSet; ``` +### Reset ActiveValue + +Calling `reset` method will convert `Unchanged` active value as `Set`. + +```rust +use sea_orm::ActiveValue; + +// An `Unchanged` value +let active_value: ActiveValue = ActiveValue::Unchanged(10); + +// Convert `Unchanged` active value as `Set` +assert!(active_value.reset(), ActiveValue::Set(10)); +``` + ## Model & ActiveModel An `ActiveModel` has all the attributes of `Model` wrapped in `ActiveValue`. diff --git a/SeaORM/docs/05-basic-crud/04-update.md b/SeaORM/docs/05-basic-crud/04-update.md index 5747fe766b1..797dec830a2 100644 --- a/SeaORM/docs/05-basic-crud/04-update.md +++ b/SeaORM/docs/05-basic-crud/04-update.md @@ -14,6 +14,30 @@ let mut pear: fruit::ActiveModel = pear.unwrap().into(); pear.name = Set("Sweet pear".to_owned()); // Update corresponding row in database using primary key value +// SQL: `UPDATE "fruit" SET "name" = 'Sweet pear' WHERE "id" = 28` +let pear: fruit::Model = pear.update(db).await?; +``` + +### Update All Fields + +You can make `Unchanged` fields "dirty". + +```rust +let pear: Option = Fruit::find_by_id(28).one(db).await?; + +// Into ActiveModel +let mut pear: fruit::ActiveModel = pear.unwrap().into(); + +// Update name attribute +pear.name = Set("Sweet pear".to_owned()); + +// Set a specific attribute as "dirty" (force update) +pear.reset(fruit::Column::CakeId); +// Or, set all attributes as "dirty" (force update) +pear.reset_all(); + +// Update corresponding row in database using primary key value +// SQL: `UPDATE "fruit" SET "name" = 'Sweet pear', "cake_id" = 10 WHERE "id" = 28` let pear: fruit::Model = pear.update(db).await?; ```