Skip to content

Commit

Permalink
Added ActiveValue::reset to convert Unchanged into Set (SeaQL/s…
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Feb 3, 2023
1 parent 4370d44 commit 4604cef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions SeaORM/docs/05-basic-crud/03-insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ let _: ActiveValue<i32> = Set(10);
let _: ActiveValue<i32> = 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<i32> = 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`.
Expand Down
24 changes: 24 additions & 0 deletions SeaORM/docs/05-basic-crud/04-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::Model> = 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?;
```

Expand Down

0 comments on commit 4604cef

Please sign in to comment.