pub enum ActiveValue<V>{
Set(V),
Unchanged(V),
NotSet,
}Expand description
The state of a field in an ActiveModel.
There are three possible states represented by three enum variants:
- Set - a value that’s explicitly set by the application and sent to the database.
- Unchanged - an existing, unchanged value from the database.
- NotSet - an undefined value (nothing is sent to the database).
The difference between these states is useful
when constructing INSERT and UPDATE SQL statements (see an example below).
It’s also useful for knowing which fields have changed in a record.
§Examples
use sea_orm::tests_cfg::{cake, fruit};
use sea_orm::{DbBackend, entity::*, query::*};
// Here, we use `NotSet` to let the database automatically generate an `id`.
// This is different from `Set(None)` that explicitly sets `cake_id` to `NULL`.
assert_eq!(
Insert::one(fruit::ActiveModel {
id: ActiveValue::NotSet,
name: ActiveValue::Set("Orange".to_owned()),
cake_id: ActiveValue::Set(None),
})
.build(DbBackend::Postgres)
.to_string(),
r#"INSERT INTO "fruit" ("name", "cake_id") VALUES ('Orange', NULL)"#
);
// Here, we update the record, set `cake_id` to the new value
// and use `NotSet` to avoid updating the `name` field.
// `id` is the primary key, so it's used in the condition and not updated.
assert_eq!(
Update::one(fruit::ActiveModel {
id: ActiveValue::Unchanged(1),
name: ActiveValue::NotSet,
cake_id: ActiveValue::Set(Some(2)),
})
.validate()
.unwrap()
.build(DbBackend::Postgres)
.to_string(),
r#"UPDATE "fruit" SET "cake_id" = 2 WHERE "fruit"."id" = 1"#
);Variants§
Set(V)
A Value that’s explicitly set by the application and sent to the database.
Use this to insert or set a specific value.
When editing an existing value, you can use set_ne to preserve the Unchanged state when the new value is the same as the old one. Then you can meaningfully use methods like crate::ActiveModelTrait::is_changed.
Unchanged(V)
An existing, unchanged Value from the database.
You get these when you query an existing Model from the database and convert it into an ActiveModel.
When you edit it, you can use set_ne to preserve this “unchanged” state if the new value is the same as the old one. Then you can meaningfully use methods like crate::ActiveModelTrait::is_changed.
NotSet
An undefined Value. Nothing is sent to the database.
When you create a new ActiveModel, its fields are NotSet by default.
This can be useful when:
- You insert a new record and want the database to generate a default value (e.g., an id).
- In an
UPDATEstatement, you don’t want to update some field.
Implementations§
Source§impl<V> ActiveValue<V>
impl<V> ActiveValue<V>
Sourcepub fn set(value: V) -> Self
pub fn set(value: V) -> Self
Create an ActiveValue::Set
Sourcepub fn is_set(&self) -> bool
pub fn is_set(&self) -> bool
Check if the ActiveValue is ActiveValue::Set
Sourcepub fn is_set_and(&self, f: impl FnOnce(&V) -> bool) -> bool
pub fn is_set_and(&self, f: impl FnOnce(&V) -> bool) -> bool
Check if the ActiveValue is ActiveValue::Set and that the inner value matches a given predicate.
Sourcepub fn unchanged(value: V) -> Self
pub fn unchanged(value: V) -> Self
Create an ActiveValue::Unchanged
Sourcepub fn is_unchanged(&self) -> bool
pub fn is_unchanged(&self) -> bool
Check if the ActiveValue is ActiveValue::Unchanged
Sourcepub fn is_unchanged_and(&self, f: impl FnOnce(&V) -> bool) -> bool
pub fn is_unchanged_and(&self, f: impl FnOnce(&V) -> bool) -> bool
Check if the ActiveValue is ActiveValue::Unchanged and that the inner value matches a given predicate.
Sourcepub fn not_set() -> Self
pub fn not_set() -> Self
Create an ActiveValue::NotSet
Sourcepub fn is_not_set(&self) -> bool
pub fn is_not_set(&self) -> bool
Check if the ActiveValue is ActiveValue::NotSet
Sourcepub fn take(&mut self) -> Option<V>
pub fn take(&mut self) -> Option<V>
Take ownership of the inner value, also setting self to NotSet
Sourcepub fn into_value(self) -> Option<Value>
pub fn into_value(self) -> Option<Value>
Take ownership of the inner value, consuming self
Sourcepub fn into_wrapped_value(self) -> ActiveValue<Value>
pub fn into_wrapped_value(self) -> ActiveValue<Value>
Wrap the Value into a ActiveValue<Value>
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the value from ActiveValue::Unchanged to ActiveValue::Set, leaving ActiveValue::NotSet untouched.
Sourcepub fn set_ne(&mut self, value: V)where
V: PartialEq,
pub fn set_ne(&mut self, value: V)where
V: PartialEq,
Set(value), except when self.is_unchanged()
and value equals the current Unchanged value.
This is useful when you have an Unchanged value from the database,
then update it using this method,
and then use .is_unchanged() to see whether it has actually changed.
The same nice effect applies to the entire ActiveModel.
You can now meaningfully use crate::ActiveModelTrait::is_changed
to see whether are any changes that need to be saved to the database.
§Examples
let mut value = ActiveValue::Unchanged("old");
// This wouldn't be the case if we used plain `value = Set("old");`
value.set_ne("old");
assert!(value.is_unchanged());
// Only when we change the actual `&str` value, it becomes `Set`
value.set_ne("new");
assert_eq!(value.is_unchanged(), false);
assert_eq!(value, ActiveValue::Set("new"));Sourcepub fn set_if_not_equals(&mut self, value: V)where
V: PartialEq,
pub fn set_if_not_equals(&mut self, value: V)where
V: PartialEq,
Alias for ActiveValue::set_ne. Kept for compatibility.
Sourcepub fn set_ne_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)where
V: PartialEq,
pub fn set_ne_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)where
V: PartialEq,
Set(value), except when self.is_unchanged(),
value equals the current Unchanged value, and value
does not match a given predicate.
This is useful in the same situations as ActiveValue::set_ne as
well as when you want to leave an existing Set value alone
depending on a condition, such as ensuring a None value never replaced an
existing Some value. This can come up when trying to merge two ActiveValues.
§Examples
let mut value = ActiveValue::Set(Some("old"));
// since Option::is_some(None) == false, we leave the existing set value alone
value.set_ne_and(None, Option::is_some);
assert_eq!(value, ActiveValue::Set(Some("old")));
// since Option::is_some(Some("new")) == true, we replace the set value
value.set_ne_and(Some("new"), Option::is_some);
assert_eq!(value, ActiveValue::Set(Some("new")));Sourcepub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)where
V: PartialEq,
pub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)where
V: PartialEq,
Alias for ActiveValue::set_ne_and. Kept for compatibility.
Sourcepub fn set_if_unset(&mut self, value: V)
pub fn set_if_unset(&mut self, value: V)
Set(value) if self.is_not_set(), no-op otherwise.
Similar to “null coalescing” or Option, but without
returning the inner value if it is set/unchanged.
§Examples
let mut set_value = ActiveValue::Set(true);
let mut unchanged_value = ActiveValue::Unchanged(true);
let mut notset_value = ActiveValue::NotSet;
// since `set_value.is_not_set == false`, we leave the existing set value alone
set_value.set_if_unset(false);
assert_eq!(set_value, ActiveValue::Set(true));
// since `set_value.is_not_set == false`, we leave the existing set value alone
unchanged_value.set_if_unset(false);
assert_eq!(unchanged_value, ActiveValue::Unchanged(true));
// since `set_value.is_not_set == true`, we fill with the provided value
notset_value.set_if_unset(false);
assert_eq!(notset_value, ActiveValue::Set(false));Sourcepub fn set_if_unset_with(&mut self, f: impl FnOnce() -> V)
pub fn set_if_unset_with(&mut self, f: impl FnOnce() -> V)
Set(f()) if self.is_not_set(), no-op otherwise.
Similar to “null coalescing” or Option, but without
returning the inner value if it is set/unchanged.
This can be useful if the value you want to replace it with is expensive to compute, or has side-effects which need to be ran (like logging).
§Examples
let mut set_value = ActiveValue::Set(true);
let mut unchanged_value = ActiveValue::Unchanged(true);
let mut notset_value = ActiveValue::NotSet;
let mut count = 0;
// since `set_value.is_not_set == false`, we leave the existing set value alone
set_value.set_if_unset_with(|| {
count += 1;
false
});
assert_eq!(set_value, ActiveValue::Set(true));
// since `set_value.is_not_set == false`, we leave the existing set value alone
unchanged_value.set_if_unset_with(|| {
count += 1;
false
});
assert_eq!(unchanged_value, ActiveValue::Unchanged(true));
// since `set_value.is_not_set == true`, we fill with the result of the provided computation.
notset_value.set_if_unset_with(|| {
count += 1;
false
});
assert_eq!(notset_value, ActiveValue::Set(false));
// Only the last closure actually executed.
assert_eq!(count, 1);Sourcepub fn set_if_unset_default(&mut self)where
V: Default,
pub fn set_if_unset_default(&mut self)where
V: Default,
Set(V::default()) if self.is_not_set(), no-op otherwise.
Similar to “null coalescing” or Option, but without
returning the inner value if it is set/unchanged.
Convenient shorthand for set_if_unset(Default::default()).
§Examples
let mut set_value = ActiveValue::Set(100);
let mut unchanged_value = ActiveValue::Unchanged(100);
let mut notset_value = ActiveValue::NotSet;
// since `set_value.is_not_set == false`, we leave the existing set value alone
set_value.set_if_unset_default();
assert_eq!(set_value, ActiveValue::Set(100));
// since `set_value.is_not_set == false`, we leave the existing set value alone
unchanged_value.set_if_unset_default();
assert_eq!(unchanged_value, ActiveValue::Unchanged(100));
// since `set_value.is_not_set == true`, we fill with the default int (0)
notset_value.set_if_unset_default();
assert_eq!(notset_value, ActiveValue::Set(0));Sourcepub fn try_as_ref(&self) -> Option<&V>
pub fn try_as_ref(&self) -> Option<&V>
Get the inner value, unless self is NotSet.
There’s also a panicking version: ActiveValue::as_ref.
§Examples
assert_eq!(ActiveValue::Unchanged(42).try_as_ref(), Some(&42));
assert_eq!(ActiveValue::Set(42).try_as_ref(), Some(&42));
assert_eq!(ActiveValue::NotSet.try_as_ref(), None::<&i32>);Source§impl<V> ActiveValue<Option<V>>
impl<V> ActiveValue<Option<V>>
Sourcepub fn as_option(&self) -> Option<&V>
pub fn as_option(&self) -> Option<&V>
Flatten an &ActiveValue<Option<V>> into an Option<&V>, folding
NotSet together with the inner None,
and Set / Unchanged together
with the inner Some.
Shorthand for self.try_as_ref().and_then(Option::as_ref). For the owned
version, see ActiveValue::into_option.
use sea_orm::ActiveValue;
let x: ActiveValue<Option<i32>> = ActiveValue::Set(Some(1));
let y: ActiveValue<Option<i32>> = ActiveValue::Set(None);
let z: ActiveValue<Option<i32>> = ActiveValue::NotSet;
assert_eq!(x.as_option(), Some(&1));
assert_eq!(y.as_option(), None);
assert_eq!(z.as_option(), None);
// composes cleanly with a predicate, on a single line
assert!(x.as_option().is_some_and(|v| *v == 1));Sourcepub fn into_option(self) -> Option<V>
pub fn into_option(self) -> Option<V>
Flatten an ActiveValue<Option<V>> into an Option<V>, folding
NotSet together with the inner None,
and Set / Unchanged together
with the inner Some.
For a borrowing version, see ActiveValue::as_option.
use sea_orm::ActiveValue;
let x: ActiveValue<Option<i32>> = ActiveValue::Set(Some(1));
let y: ActiveValue<Option<i32>> = ActiveValue::Set(None);
let z: ActiveValue<Option<i32>> = ActiveValue::NotSet;
assert_eq!(x.into_option(), Some(1));
assert_eq!(y.into_option(), None);
assert_eq!(z.into_option(), None);Trait Implementations§
Source§impl<V> AsRef<V> for ActiveValue<V>
impl<V> AsRef<V> for ActiveValue<V>
Source§fn as_ref(&self) -> &V
fn as_ref(&self) -> &V
§Panics
Panics if it is ActiveValue::NotSet.
See ActiveValue::try_as_ref for a fallible non-panicking version.
Source§impl<V> Clone for ActiveValue<V>
impl<V> Clone for ActiveValue<V>
Source§fn clone(&self) -> ActiveValue<V>
fn clone(&self) -> ActiveValue<V>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<V> Debug for ActiveValue<V>
impl<V> Debug for ActiveValue<V>
Source§impl<V> Default for ActiveValue<V>
impl<V> Default for ActiveValue<V>
Source§fn default() -> Self
fn default() -> Self
Create an ActiveValue::NotSet
Source§impl<V> DefaultActiveValue for ActiveValue<V>
impl<V> DefaultActiveValue for ActiveValue<V>
Source§fn default_value(&self) -> Self
fn default_value(&self) -> Self
Default::default() if implemented, dummy value otherwise.Source§impl<V> DefaultActiveValueNone for ActiveValue<Option<V>>
impl<V> DefaultActiveValueNone for ActiveValue<Option<V>>
Source§fn default_value(&self) -> Self
fn default_value(&self) -> Self
None.Source§impl<V> DefaultActiveValueNotSet for &ActiveValue<V>
impl<V> DefaultActiveValueNotSet for &ActiveValue<V>
Source§type Value = ActiveValue<V>
type Value = ActiveValue<V>
Source§fn default_value(&self) -> Self::Value
fn default_value(&self) -> Self::Value
NotSet.Source§impl<V> From<ActiveValue<V>> for ActiveValue<Option<V>>
impl<V> From<ActiveValue<V>> for ActiveValue<Option<V>>
Source§fn from(value: ActiveValue<V>) -> Self
fn from(value: ActiveValue<V>) -> Self
Auto Trait Implementations§
impl<V> Freeze for ActiveValue<V>where
V: Freeze,
impl<V> RefUnwindSafe for ActiveValue<V>where
V: RefUnwindSafe,
impl<V> Send for ActiveValue<V>where
V: Send,
impl<V> Sync for ActiveValue<V>where
V: Sync,
impl<V> Unpin for ActiveValue<V>where
V: Unpin,
impl<V> UnsafeUnpin for ActiveValue<V>where
V: UnsafeUnpin,
impl<V> UnwindSafe for ActiveValue<V>where
V: UnwindSafe,
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more