diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 983082bec4..ea83d97bc4 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -92,7 +92,7 @@ pub trait ActiveModelTrait: Clone + Debug { } async fn update(self, db: &DatabaseConnection) -> Result { - let exec = Self::Entity::update(self).prepare_filters().exec(db); + let exec = Self::Entity::update(self).exec(db); exec.await } diff --git a/src/query/update.rs b/src/query/update.rs index 3500b75940..2ee9333cfe 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -37,7 +37,6 @@ impl Update { /// id: ActiveValue::set(1), /// name: ActiveValue::set("Apple Pie".to_owned()), /// }) - /// .filter(cake::Column::Id.eq(1)) /// .build(DbBackend::Postgres) /// .to_string(), /// r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#, @@ -48,12 +47,13 @@ impl Update { E: EntityTrait, A: ActiveModelTrait, { - let myself = UpdateOne { + let mut myself = UpdateOne { query: UpdateStatement::new() .table(A::Entity::default().table_ref()) .to_owned(), model, }; + myself = myself.prepare_filters(); myself.prepare_values() } @@ -86,19 +86,6 @@ impl UpdateOne where A: ActiveModelTrait, { - pub(crate) fn prepare_values(mut self) -> Self { - for col in ::Column::iter() { - if ::PrimaryKey::from_column(col).is_some() { - continue; - } - let av = self.model.get(col); - if av.is_set() { - self.query.value(col, av.unwrap()); - } - } - self - } - pub(crate) fn prepare_filters(mut self) -> Self { for key in ::PrimaryKey::iter() { let col = key.into_column(); @@ -111,6 +98,19 @@ where } self } + + pub(crate) fn prepare_values(mut self) -> Self { + for col in ::Column::iter() { + if ::PrimaryKey::from_column(col).is_some() { + continue; + } + let av = self.model.get(col); + if av.is_set() { + self.query.value(col, av.unwrap()); + } + } + self + } } impl QueryFilter for UpdateOne @@ -199,7 +199,6 @@ mod tests { id: ActiveValue::set(1), name: ActiveValue::set("Apple Pie".to_owned()), }) - .filter(cake::Column::Id.eq(1)) .build(DbBackend::Postgres) .to_string(), r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#, @@ -214,7 +213,6 @@ mod tests { name: ActiveValue::set("Orange".to_owned()), cake_id: ActiveValue::unset(), }) - .filter(fruit::Column::Id.eq(1)) .build(DbBackend::Postgres) .to_string(), r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#, @@ -241,7 +239,6 @@ mod tests { assert_eq!( Update::many(fruit::Entity) .col_expr(fruit::Column::CakeId, Expr::value(Value::Int(None))) - .filter(fruit::Column::Id.eq(2)) .build(DbBackend::Postgres) .to_string(), r#"UPDATE "fruit" SET "cake_id" = NULL WHERE "fruit"."id" = 2"#,