diff --git a/sea-orm-macros/src/derives/active_model.rs b/sea-orm-macros/src/derives/active_model.rs index 17c6c50c..81c91c7a 100644 --- a/sea-orm-macros/src/derives/active_model.rs +++ b/sea-orm-macros/src/derives/active_model.rs @@ -41,7 +41,7 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result for ActiveModel { fn from(m: #ident) -> Self { Self { - #(#field: sea_orm::ActiveValue::set(m.#field)),* + #(#field: sea_orm::unchanged_active_value_not_intended_for_public_use(m.#field)),* } } } diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 50b5461d..8bd53c4d 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -11,8 +11,9 @@ where } #[derive(Clone, Debug)] -pub enum ActiveValueState { +enum ActiveValueState { Set, + Unchanged, Unset, } @@ -22,6 +23,13 @@ impl Default for ActiveValueState { } } +pub fn unchanged_active_value_not_intended_for_public_use(value: V) -> ActiveValue +where + V: Default, +{ + ActiveValue::unchanged(value) +} + impl ActiveValue where V: Default, @@ -37,6 +45,13 @@ where matches!(self.state, ActiveValueState::Set) } + pub(crate) fn unchanged(value: V) -> Self { + Self { + value, + state: ActiveValueState::Unchanged, + } + } + pub fn unset() -> Self { Self { value: V::default(), @@ -58,6 +73,15 @@ where } } +impl std::convert::AsRef for ActiveValue +where + V: Default, +{ + fn as_ref(&self) -> &V { + &self.value + } +} + impl ActiveValue where V: Default + Into, @@ -69,6 +93,7 @@ where pub fn into_wrapped_value(self) -> ActiveValue { match self.state { ActiveValueState::Set => ActiveValue::set(self.into_value()), + ActiveValueState::Unchanged => ActiveValue::set(self.into_value()), ActiveValueState::Unset => ActiveValue::unset(), } } diff --git a/src/operation/insert.rs b/src/operation/insert.rs index 5dfeb19c..2d317c3f 100644 --- a/src/operation/insert.rs +++ b/src/operation/insert.rs @@ -37,7 +37,7 @@ where let mut values = Vec::new(); for col in A::Column::iter() { let av = am.take(col); - if av.is_set() { + if !av.is_unset() { columns.push(col); values.push(av.into_value()); }