Update records
Rwf allows to update records using two mechanisms:
- Update a single record by calling
Model::save
on an instance of a model - Update multiple records using one query by using
Model::update_all
Update a single record
Updating a single record can be done by mutating struct instance fields and calling save
:
Instead of fetching the record from the database, you can just instantiate one manually, as long as you know the desired primary key value:
let user = User {
id: Some(25),
email: "new_email@example.com",
created_at: OffsetDateTime::now_utc(),
};
let user = user
.save()
.fetch(&mut conn)
.await?;
This is very similar to creating new records, except that we set the id
field to a known value.
When the id
is set to Some(i64)
, Rwf assumes the record exists in the database, meanwhile if the id
is None
, Rwf will attempt to create one instead.
Update multiple records
Updating multiple records in one query is possible by searching for them first and then calling update_all
: