sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

DeriveIntoActiveModel doesn't work with Enums

Open Stefan2409 opened this issue 2 years ago • 1 comments

I have created the following entity model:


use super::sea_orm_active_enums::*;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "user")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    #[sea_orm(unique)]
    pub email: String,
    #[sea_orm(column_type = "Text")]
    pub password: String,
    pub role: Role,
    pub first_name: String,
    pub last_name: String,
    pub date_of_birth: Date,
    pub image_url: Option<String>,
    pub created_at: Option<DateTime>,
    pub updated_at: Option<DateTime>,
    pub created_from: Option<i64>,
    pub updated_from: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Deserialize, Debug, Validate, DeriveIntoActiveModel)]
pub struct UpdateUser {
    #[validate(email)]
    email: String,
    #[validate(length(max = 50))]
    first_name: String,
    #[validate(length(max = 50))]
    last_name: String,
    date_of_birth: Date,
    role: Role,
}

with the following enum definition for Role:

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

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "role")]
pub enum Role {
    #[sea_orm(string_value = "Admin")]
    Admin,
    #[sea_orm(string_value = "User")]
    User,
}

The Problem is that I'm getting the error: the trait bound sea_orm_active_enums::Role: IntoActiveValue<_> is not satisfied with my UpdateUser struct.

The trait bound should be satisfied already. If I write a User with the whole Model to the DB everything works fine. So there's something missing in the DeriveIntoActiveModel implementation with enums.

I'm using sea-orm 12.6.

Stefan2409 avatar Nov 17 '23 08:11 Stefan2409

Maybe this is because the enum doesn't implement IntoActiveValue? Is there any easier way to implement IntoActiveValue for generated enums instead of doing it manually?

Huliiiiii avatar Nov 25 '24 12:11 Huliiiiii