Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated Rust enum ordering does not match underlying database enum ordering #2285

Open
foxfriends opened this issue Jul 12, 2024 · 1 comment · May be fixed by SeaQL/sea-schema#138
Open

Comments

@foxfriends
Copy link

foxfriends commented Jul 12, 2024

Description

At least for Postgres (I have no experience with other databases) enums are ordered by definition order, but sea-orm-cli generate generates enums in alphabetical order. This is particularly problematic when I add --enum-extra-derives 'Ord,PartialOrd' --enum-extra-attributes 'repr(u32)' and now the rust-level comparison operator is not the same as the db-level comparison, and neither is the underlying u32 repr the same. This is also annoying if trying to use ActiveEnum::iter() but the ordering is not the meaningful order defined in the enum, but the alphabetical order of the Rust enum.

Steps to Reproduce

  1. Define an enum e.g. for months of the year
CREATE TYPE month AS ENUM (
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
);
  1. Generate the enum using

sea-orm-cli generate entity -o ./entity --with-serde both --enum-extra-derives 'Copy,Ord,PartialOrd' --enum-extra-attributes 'repr(u32)'

#[derive(
    Debug,
    Clone,
    PartialEq,
    Eq,
    EnumIter,
    DeriveActiveEnum,
    Serialize,
    Deserialize,
    Copy,
    Ord,
    PartialOrd,
)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "month")]
#[repr(u32)]
pub enum Month {
    #[sea_orm(string_value = "April")]
    April,
    #[sea_orm(string_value = "August")]
    August,
    #[sea_orm(string_value = "December")]
    December,
    #[sea_orm(string_value = "February")]
    February,
    #[sea_orm(string_value = "January")]
    January,
    #[sea_orm(string_value = "July")]
    July,
    #[sea_orm(string_value = "June")]
    June,
    #[sea_orm(string_value = "March")]
    March,
    #[sea_orm(string_value = "May")]
    May,
    #[sea_orm(string_value = "November")]
    November,
    #[sea_orm(string_value = "October")]
    October,
    #[sea_orm(string_value = "September")]
    September,
}

Expected Behavior

The Rust enum should match the order of the Postgres enum.

Actual Behavior

The Rust enum is in alphabetical order.

Reproduces How Often

Always

Workarounds

Manually implement the enum separately.

Versions

├── sea-orm v0.12.15
│   ├── sea-orm-macros v0.12.15 (proc-macro)
│   │   ├── sea-bae v0.2.0 (proc-macro)
│   ├── sea-query v0.30.7
│   │   ├── sea-query-derive v0.4.1 (proc-macro)
│   ├── sea-query-binder v0.5.0
│   │   ├── sea-query v0.30.7 (*)
├── sea-query v0.30.7 (*)

Using postgres:latest in Docker on Mac.

@Expurple
Copy link
Contributor

The Rust enum should match the order of the Postgres enum.

Actually, enum ordering differs across databases. See #2132. For SQLite, SeaORM emulates enums as strings and SQLite compares them alphabetically. So, the current Rust behavior is consistent with SQLite. I'm not sure which default makes more sense. This should probably be configurable.

@TheDIM47 TheDIM47 linked a pull request Sep 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants