Trait diesel::query_dsl::SaveChangesDsl

source ·
pub trait SaveChangesDsl<Conn> {
    // Provided method
    fn save_changes<T>(self, connection: &mut Conn) -> QueryResult<T>
       where Self: Sized,
             Conn: UpdateAndFetchResults<Self, T> { ... }
}
Expand description

Sugar for types which implement both AsChangeset and Identifiable

On backends which support the RETURNING keyword, foo.save_changes(&conn) is equivalent to update(&foo).set(&foo).get_result(&conn). On other backends, two queries will be executed.

§Example

#[derive(Queryable, Debug, PartialEq)]
struct Animal {
   id: i32,
   species: String,
   legs: i32,
   name: Option<String>,
}

#[derive(AsChangeset, Identifiable)]
#[diesel(table_name = animals)]
struct AnimalForm<'a> {
    id: i32,
    name: &'a str,
}

let form = AnimalForm { id: 2, name: "Super scary" };
let changed_animal = form.save_changes(connection)?;
let expected_animal = Animal {
    id: 2,
    species: String::from("spider"),
    legs: 8,
    name: Some(String::from("Super scary")),
};
assert_eq!(expected_animal, changed_animal);

Provided Methods§

source

fn save_changes<T>(self, connection: &mut Conn) -> QueryResult<T>
where Self: Sized, Conn: UpdateAndFetchResults<Self, T>,

See the trait documentation.

Implementors§

source§

impl<T, Conn> SaveChangesDsl<Conn> for T
where T: Copy + AsChangeset<Target = <T as HasTable>::Table> + IntoUpdateTarget,