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

SeaORM CLI not adding serde derives to Enums #461

Closed BenJeau closed 2 years ago

BenJeau commented 2 years ago

I'm currently playing with sea-orm coming from diesel-rs and I love it! But I ran into this error while using enums and serde, the enums do not contain any serde derives.

Reproduction

With a simple PostgreSQL schema

CREATE TYPE "user_kind_type" AS ENUM ('host', 'client');

CREATE TABLE users (
  "id" SERIAL PRIMARY KEY,
  "kind" "user_kind_type" NOT NULL
);

Running the following command (same behaviour with either serialize, deserialize, both):

sea-orm-cli generate entity -o src/entities --with-serde serialize

Creates the following file with Enums that does not contain the serde derives, where the errors propagates to the users model's kind column.

//! SeaORM Entity. Generated by sea-orm-codegen 0.5.0

use sea_orm::entity::prelude::*;

#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "user_kind_type")]
pub enum UserKindType {
    #[sea_orm(string_value = "client")]
    Client,
    #[sea_orm(string_value = "host")]
    Host,
}

Expected Result

I would expect enums, just like models, to contain the derive.

//! SeaORM Entity. Generated by sea-orm-codegen 0.5.0

use sea_orm::entity::prelude::*;
use serde::Serialize;

#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Serialize)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "user_kind_type")]
pub enum UserKindType {
    #[sea_orm(string_value = "client")]
    Client,
    #[sea_orm(string_value = "host")]
    Host,
}
BenJeau commented 2 years ago

Just by looking at the source code, seems like enums are not treating imports the same way as entities.

Enums (https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/writer.rs#L156) don't seem to care about serde whereas entities are using this function for generating imports (https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/writer.rs#L231).

And as for the derives, entities take care of this here https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/writer.rs#L509, whereas enums are not https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/active_enum.rs#L21.

Seems like it has been forgotten unless it was intentional? I can look into creating a PR adding serde derives to enums if it is wanted in this crate.

billy1624 commented 2 years ago

Seems like it has been forgotten unless it was intentional?

I forgot to do so :(

I can look into creating a PR adding serde derives to enums if it is wanted in this crate.

Definitely wanted