pub enum ActiveValue<V>{
Set(V),
Unchanged(V),
NotSet,
}Expand description
The state of a field in an [ActiveModel][ActiveModelTrait].
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_if_not_equals to preserve the Unchanged state when the new value is the same as the old one. Then you can meaningfully use methods like [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][ActiveModelTrait].
When you edit it, you can use set_if_not_equals to preserve this โunchangedโ state if the new value is the same as the old one. Then you can meaningfully use methods like [ActiveModelTrait::is_changed].
NotSet
An undefined Value. Nothing is sent to the database.
When you create a new [ActiveModel][ActiveModelTrait], 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 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 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_if_not_equals(&mut self, value: V)where
V: PartialEq,
pub fn set_if_not_equals(&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 [ActiveModelTrait::is_changed][ActiveModelTrait#method.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_if_not_equals("old");
assert!(value.is_unchanged());
// Only when we change the actual `&str` value, it becomes `Set`
value.set_if_not_equals("new");
assert_eq!(value.is_unchanged(), false);
assert_eq!(value, ActiveValue::Set("new"));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>);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 ยท 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 otherwiseSourceยง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
NoneSourceยง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
NotSetSourceยง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
Sourceยงimpl<V> PartialEq for ActiveValue<V>
impl<V> PartialEq for ActiveValue<V>
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> UnwindSafe for ActiveValue<V>where
V: UnwindSafe,
Blanket Implementationsยง
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
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