DoDr
DoDr

Reputation: 133

Sea Orm - Actix , vote system

Django user here. I switching to Actix written in rust. How to add or subtract 1 from existed value in database. I have function

    post.vote = ActiveValue::Set(post.vote.into_wrapped_value() + 1);

and also tried

 post.vote = ActiveValue::Set(post.vote.take() + 1);

But i get error cannot subtract {integer} from std::option::Option<i32>

 post.vote = ActiveValue::Set(<sea_orm::ActiveValue<i32> as Into<T>>::into(post.vote) + 1);

Cannot find type T in this scope so i add to <T> to --> impl<T> PostRepository {

the type parameter T is not constrained by the impl trait, self type, or predicates unconstrained type parameter

Any hint

Upvotes: 1

Views: 310

Answers (1)

Nickniki
Nickniki

Reputation: 11

From documents https://www.sea-ql.org/SeaORM/docs/basic-crud/update/ People can first query exist value from database and then update it.

In your case, the codes could be like:

let pose = Pose::find_by_id(pose_id).one(db).await?.unwrap();
let vote = pose.vote;
let mut pose: pose::ActiveModel = pose.into();
pose.vote = Set(vote.map(|v| v + 1));

let pose: pose::Model = pose.update(db).await?;

Upvotes: 1

Related Questions