Skip to main content

ActiveModelTrait

Trait ActiveModelTrait 

Source
pub trait ActiveModelTrait: Send + Sync {
    // Required methods
    fn table_name(&self) -> &str;
    fn pk_value(&self) -> Option<Value>;
    fn for_each_changed<F>(&self, f: F)
       where F: FnMut(&str, &ActiveValue<Value>);
}
Expand description

ActiveModel 行为 trait

实现此 trait 的类型可以:

  1. 暴露变更字段列表(供 UPDATE 使用)
  2. 提供主键值(供 WHERE 条件使用)
  3. 提供表名(供 SQL 生成使用)

§Model trait 的关系

Model 描述的是“完整行记录“的静态元数据(表名、主键列名等); ActiveModel 描述的是“待持久化的变更集“的动态状态。 两者正交:一个 Model 实例是全量快照,一个 ActiveModel 实例是增量变更。

Required Methods§

Source

fn table_name(&self) -> &str

获取表名

Source

fn pk_value(&self) -> Option<Value>

获取主键值(用于 WHERE 条件)

Source

fn for_each_changed<F>(&self, f: F)
where F: FnMut(&str, &ActiveValue<Value>),

遍历所有已设置(Set)的字段

回调 f 对每个变更字段调用,传入字段名和 ActiveValue<Value> 引用。 实现方负责过滤出 is_set() == true 的字段。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§