Use Partial Models for efficient operations
mainPartial models allow you to work with a subset of fields from a complete model. This is useful for performance (reading/writing only what is needed) and type safety (enforcing which fields are required for specific tasks).
Creating a Partial Model
Use the partial_<model_name>! macro (auto-generated in the module where the original model is defined) to create a new struct.
Requirements:
- The original model must implement
#[derive(Default)]. - The partial model definition must include all primary key fields.
- The macro should be used in the same file as the original model.
Converting to Native Model
If you have a partial model but need to perform an operation that requires the full model, use the .as_native() method.
// Create a partial model with only 'id' and 'username'
partial_user!(UpdateUsernameUser, id, username);
let mut update_user_username = UpdateUsernameUser {
id,
username: "updated_username".to_string(),
};
// Perform update
update_user_username.update().execute(&session).await?;
// Convert back to native model if needed
let native_user: User = update_user_username.as_native()
.find_by_primary_key()
.execute(&session)
.await?;