From c5e023c2e808632aa42536ac24228da10dc2d643 Mon Sep 17 00:00:00 2001 From: Izzy McCabe Date: Tue, 2 Dec 2025 12:11:48 -0800 Subject: [PATCH] Add notes on converting from ActiveModel to Model --- SeaORM/docs/05-basic-crud/03-active-model.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/SeaORM/docs/05-basic-crud/03-active-model.md b/SeaORM/docs/05-basic-crud/03-active-model.md index 33508679f71..a2e1ff165c3 100644 --- a/SeaORM/docs/05-basic-crud/03-active-model.md +++ b/SeaORM/docs/05-basic-crud/03-active-model.md @@ -126,3 +126,23 @@ assert_eq!( Err(DbErr::AttrNotSet(String::from("name"))) ); ``` + +This will fail if any of the fields of the ActiveModel are `NotSet`. +If you wish to automatically fill remaining fields with their `Default::default()` values (or fallback to `NotSet` if they don't have one), you can use the +[`ActiveModelTrait::default_values()`](https://docs.rs/sea-orm/*/sea_orm/entity/trait.ActiveModelTrait.html#tymethod.default_values) method. +This can be useful for quickly creating mock models for testing purposes. +```rust +assert_eq!( + ActiveModel { + id: Set(2), + ..ActiveModel::default_values() + } + .try_into_model() + .unwrap(), + Model { + id: 2, + name: String::default(), // empty string, + cake_id: Option::default(), // `None` + } +); +```