SeaQL/sea-orm

`AttrNotSet` error when using `.try_into_model()` even though the attribute listed is optional

开放

#2,834 创建于 2025年12月2日

 (3 条评论) (0 个反应) (1 位负责人)Rust (718 个派生)auto 404
good first issue

仓库指标

星标
 (9,827 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

Description

When creating models as mock inputs for testing purposes, I am creating an ActiveModel with only the non-nullable fields filled out and using default to make the rest NotSet, then using .try_into_model() to convert that into a model (which is required by my function). When I run the test, I get an AttrNotSet error complaining that I haven't set the optional fields, even though these should default to None.

Steps to Reproduce

Model definition:

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "stations_creston")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub station_id: i32,
    #[sea_orm(unique)]
    pub network_id: String,
    pub station_name: String,
    pub installation_date: Option<TimeDate>,
}

It should also be noted that installation_date is set to nullable in my MySQL database, and the default value is set to NULL

Test setup

let test_station = stations_creston::ActiveModel {
    station_id: ActiveValue::Set(628),
    network_id: ActiveValue::Set("CRESNEKN".to_owned()),
    station_name: ActiveValue::Set("example".to_owned()),
    ..Default::default()
}
.try_into_model()
.expect("couldn't make test station model");

Expected Behavior

This shouldn't panic

Actual Behavior

The following panic is produced:

thread 'apis::creston::test::get_data' panicked at src/apis/creston.rs:181:10:
couldn't make test station model: AttrNotSet("installation_date")

Reproduces How Often

Happens every time

Versions

SeaOrm 2.0.0-rc.18 cargo 1.93.0-nightly Void Linux

贡献者指南