pub trait SqlModelUpdate:
Model
+ Serialize
+ DeserializeOwned {
// Provided methods
fn sqlmodel_update(
&mut self,
input: impl Into<UpdateInput>,
options: UpdateOptions,
) -> ValidateResult<()> { ... }
fn sqlmodel_update_dict(
&mut self,
dict: HashMap<String, Value>,
) -> ValidateResult<()> { ... }
fn sqlmodel_update_from(
&mut self,
source: &Self,
options: UpdateOptions,
) -> ValidateResult<()>
where Self: Sized { ... }
}Expand description
Trait for models that support sqlmodel_update().
This enables updating a model instance from a dictionary or another model’s values.
§Example
use sqlmodel_core::validate::{SqlModelUpdate, UpdateInput, UpdateOptions};
let mut user = User { id: 1, name: "Alice".to_string(), age: 30 };
// Update from a HashMap
user.sqlmodel_update(
HashMap::from([("name".to_string(), serde_json::json!("Bob"))]),
UpdateOptions::default()
)?;
assert_eq!(user.name, "Bob");
// Update only specific fields
user.sqlmodel_update(
HashMap::from([
("name".to_string(), serde_json::json!("Carol")),
("age".to_string(), serde_json::json!(25))
]),
UpdateOptions::default().update_fields(["name"])
)?;
assert_eq!(user.name, "Carol");
assert_eq!(user.age, 30); // age was not updatedProvided Methods§
Sourcefn sqlmodel_update(
&mut self,
input: impl Into<UpdateInput>,
options: UpdateOptions,
) -> ValidateResult<()>
fn sqlmodel_update( &mut self, input: impl Into<UpdateInput>, options: UpdateOptions, ) -> ValidateResult<()>
Update a model instance from input.
This method merges values from the input into the current model.
Only fields present in the input (and allowed by update_fields option)
are updated.
§Arguments
input- The source of update values (Dict or JsonValue)options- Update options controlling which fields to update
§Returns
Ok(()) if the update succeeds, or a validation error if the resulting model fails validation.
Sourcefn sqlmodel_update_dict(
&mut self,
dict: HashMap<String, Value>,
) -> ValidateResult<()>
fn sqlmodel_update_dict( &mut self, dict: HashMap<String, Value>, ) -> ValidateResult<()>
Update a model instance from a HashMap with default options.
Sourcefn sqlmodel_update_from(
&mut self,
source: &Self,
options: UpdateOptions,
) -> ValidateResult<()>where
Self: Sized,
fn sqlmodel_update_from(
&mut self,
source: &Self,
options: UpdateOptions,
) -> ValidateResult<()>where
Self: Sized,
Copy non-None/non-null values from another model into this one.
This is useful for partial updates where you have a “patch” model with only the fields that should be updated (non-None values).
§Arguments
source- The source model to copy values fromoptions- Update options controlling which fields to update
§Example
let mut user = User { id: 1, name: "Alice".to_string(), age: Some(30) };
let patch = User { id: 0, name: "Bob".to_string(), age: None };
// Only update name, skip None age
user.sqlmodel_update_from(&patch, UpdateOptions::default())?;
assert_eq!(user.name, "Bob");
assert_eq!(user.age, Some(30)); // Unchanged because patch.age is NoneDyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl<T: Model + Serialize + DeserializeOwned> SqlModelUpdate for T
Blanket implementation for all Model types that implement Serialize + DeserializeOwned.