SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.94k stars 483 forks source link

Cannot derive `DeriveIntoActiveModel` for fields wrapped with `Option` #322

Closed YoshieraHuang closed 2 years ago

YoshieraHuang commented 2 years ago

I meet compiling error when I use DeriveIntoActiveModel to convert the struct to ActiveModel. The struct has only part of fields and these fields are wrapped with Option. Here is an example script:

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "products")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub name: String,
    pub price: f64
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(DeriveIntoActiveModel, Deserialize)]
pub struct UpdateProducts {
    pub name: Option<String>,
    pub price: Option<f64>
}

And here is the detailed compiling error:

the trait bound `ActiveValue<String>: From<ActiveValue<std::option::Option<String>>>` is not satisfied
the following implementations were found:
  <ActiveValue<std::option::Option<V>> as From<ActiveValue<V>>>
required because of the requirements on the impl of `Into<ActiveValue<String>>` for `ActiveValue<std::option::Option<String>>`
the trait bound `ActiveValue<f32>: From<ActiveValue<std::option::Option<f32>>>` is not satisfied
the following implementations were found:
  <ActiveValue<std::option::Option<V>> as From<ActiveValue<V>>>
required because of the requirements on the impl of `Into<ActiveValue<f32>>` for `ActiveValue<std::option::Option<f32>>`

The error says the Option<T> is converted to ActiveValue<Option<T>>. But I want Option<T> to be converted to ActiveValue<T> in my use case.

I think the associated code is here: https://github.com/SeaQL/sea-orm/blob/f2a774573a962426d5c4ac9a5abb74d5b63313b9/src/entity/active_model.rs#L301-L308

I fix the bug and I will issue a PR.

tyt2y3 commented 2 years ago

Much explanation is provided in the closed PR