Skip to content

Commit

Permalink
Fix: update many cast enum values (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Dec 19, 2022
1 parent d205338 commit 17ed715
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ where
for col in E::Column::iter() {
let av = model.get(col);
if av.is_set() {
self.query.value(col, av.unwrap());
let expr = cast_text_as_enum(Expr::val(av.into_value().unwrap()), &col);
self.query.value(col, expr);
}
}
self
Expand All @@ -207,7 +208,7 @@ where

#[cfg(test)]
mod tests {
use crate::tests_cfg::{cake, fruit};
use crate::tests_cfg::{cake, fruit, lunch_set, sea_orm_active_enums::Tea};
use crate::{entity::*, query::*, DbBackend};
use sea_query::{Expr, Value};

Expand Down Expand Up @@ -294,4 +295,33 @@ mod tests {
r#"UPDATE "fruit" SET "id" = 3 WHERE "fruit"."id" = 2"#,
);
}

#[test]
fn update_7() {
assert_eq!(
Update::many(lunch_set::Entity)
.set(lunch_set::ActiveModel {
tea: Set(Tea::EverydayTea),
..Default::default()
})
.filter(lunch_set::Column::Tea.eq(Tea::BreakfastTea))
.build(DbBackend::Postgres)
.to_string(),
r#"UPDATE "lunch_set" SET "tea" = CAST('EverydayTea' AS tea) WHERE "lunch_set"."tea" = CAST('BreakfastTea' AS tea)"#,
);
}

#[test]
fn update_8() {
assert_eq!(
Update::one(lunch_set::ActiveModel {
id: Unchanged(1),
tea: Set(Tea::EverydayTea),
..Default::default()
})
.build(DbBackend::Postgres)
.to_string(),
r#"UPDATE "lunch_set" SET "tea" = CAST('EverydayTea' AS tea) WHERE "lunch_set"."id" = 1"#,
);
}
}

0 comments on commit 17ed715

Please sign in to comment.