Function diesel::dsl::update

source ·
pub fn update<T: IntoUpdateTarget>(
    source: T
) -> UpdateStatement<T::Table, T::WhereClause>
Expand description

Creates an UPDATE statement.

When a table is passed to update, every row in the table will be updated. You can narrow this scope by calling filter on the table before passing it in, which will result in UPDATE your_table SET ... WHERE args_to_filter.

Passing a type which implements Identifiable is the same as passing some_table.find(some_struct.id()).

Examples

let updated_row = diesel::update(users.filter(id.eq(1)))
    .set(name.eq("James"))
    .get_result(connection);
// On backends that support it, you can call `get_result` instead of `execute`
// to have `RETURNING *` automatically appended to the query. Alternatively, you
// can explicitly return an expression by using the `returning` method before
// getting the result.
assert_eq!(Ok((1, "James".to_string())), updated_row);

To update multiple columns, give set a tuple argument:


let updated_row = diesel::update(users.filter(id.eq(1)))
    .set((name.eq("James"), surname.eq("Bond")))
    .get_result(connection);

assert_eq!(Ok((1, "James".to_string(), "Bond".to_string())), updated_row);