Repository navigation
Specify rs_type on generated enum #3260
|
Hello there I want to be able to specify my Sample code //src/entities/sea_orm_active_enums.rs
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "Enum", db_type = "Enum", enum_name = "action")] // rs_type = String here
#[serde(rename_all = "snake_case")]
pub enum Action {
#[sea_orm(string_value = "send_to_review")]
SendToReview,
#[sea_orm(string_value = "auto_hide")]
AutoHide,
} |
Replies: 1 comment 1 reply
|
You can do it with sea-orm-cli generate entity ... --enum-extra-attributes 'sea_orm(rs_type = "String")'The generator always writes I compiled this against current main (2.0.4) and #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "Enum", db_type = "Enum", enum_name = "action")]
#[sea_orm(rs_type = "String")]
pub enum Action {
#[sea_orm(string_value = "send_to_review")]
SendToReview,
#[sea_orm(string_value = "auto_hide")]
AutoHide,
}I didn't run the CLI against a database, and the override relies on the last attribute winning rather than a documented option, so a dedicated flag would be the cleaner long-term fix. |
You can do it with
--enum-extra-attributes, without touching the generated file:sea-orm-cli generate entity ... --enum-extra-attributes 'sea_orm(rs_type = "String")'The generator always writes
rs_type = "Enum"(active_enum.rs), but extra attributes go on the line right after it.DeriveActiveEnumreads every#[sea_orm(...)]on the enum in order and the laterrs_typeoverwrites the earlier one (derives/active_enum.rs). Withdb_type = "Enum",Stringis one of the two allowed values.I compiled this against current main (2.0.4) and
Action::AutoHide.to_value()returned aString("auto_hide"):