Change the current value?

55 Views Asked by At

How can I perform the following SQL statement in Rust with sea-orm?

UPDATE user SET balance = balance - debit

I've tried the following:

let updated = user::ActiveModel::update(&db)
    .set_expr(user::Column::Balance, user::Column::Balance - 100)
    .exec()
    .await.unwrap_or_default();

But this option doesn't work.

1

There are 1 best solutions below

0
On

I found the answer:

User::update_many()
        .col_expr(user::Column::Balance, Expr::col(user::Column::Balance).add(sum_num))
        .filter(user::Column::UserId.eq(user_id))
        .exec(&db)
        .await.unwrap_or_default();