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

Composite primary key with enum causes generated entity to fail to compile #1364

Closed
Pure-Peace opened this issue Jan 4, 2023 · 1 comment · Fixed by #1414
Closed

Composite primary key with enum causes generated entity to fail to compile #1364

Pure-Peace opened this issue Jan 4, 2023 · 1 comment · Fixed by #1414
Assignees
Milestone

Comments

@Pure-Peace
Copy link

Pure-Peace commented Jan 4, 2023

Description

Composite primary key with enum causes generated entity to fail to compile.

Entities generated from the database:

/* `sea_orm_active_enums.rs */

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.5

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(
    rs_type = "String",
    db_type = "Enum",
    enum_name = "version"
)]
pub enum Version {
    #[sea_orm(string_value = "v1")]
    V1,
    #[sea_orm(string_value = "v2")]
    V2,
}
/* `report.rs */

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.5

use super::sea_orm_active_enums::Version;
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "report")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: i64,
    #[sea_orm(primary_key, auto_increment = false)]
    pub version: Version
}

The generated entity fails to compile with the following error:

error[E0277]: the trait bound `sea_orm_active_enums::Version: TryFromU64` is not satisfied
  --> demo.rs
   |
   | #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
   |                                   ^^^^^^^^^^^^^^^^^ the trait `TryFromU64` is not implemented for `sea_orm_active_enums::Version`
   |
   = help: the following other types implement trait `TryFromU64`:
             (A, B)
             (A, B, C)
             (A, B, C, D)
             (A, B, C, D, E)
             (A, B, C, D, E, F)
             JsonValue
             Vec<u8>
             bool
           and 23 others
   = note: required for `(i64, sea_orm_active_enums::Version)` to implement `TryFromU64`
note: required by a bound in `sea_orm::PrimaryKeyTrait::ValueType`
  --> sea-orm-0.10.5\src\entity\primary_key.rs:49:11
   |
49 |         + TryFromU64;
   |           ^^^^^^^^^^ required by this bound in `sea_orm::PrimaryKeyTrait::ValueType`
   = note: this error originates in the derive macro `DeriveEntityModel` (in Nightly builds, run with -Z macro-backtrace for more info)

Steps to Reproduce

Create a table and execute migrate up

use sea_orm_migration::prelude::*;

#[derive(Iden)]
pub enum Report {
    Table,
    Id,
    Version,
}

#[derive(Iden)]
pub enum Version {
    #[iden = "version"]
    Enum = -1,
    #[iden = "v1"]
    V1,
    #[iden = "v2"]
    V2,
}

pub fn create_table() -> TableCreateStatement {
    Table::create()
        .table(Report::Table)
        .if_not_exists()
        .col(ColumnDef::new(Report::Id).big_integer().not_null())
        .col(
            ColumnDef::new(Report::Version)
                .enumeration(Version::Enum, [Version::V1, Version::V2])
                .not_null()
                .default(Version::V1.to_string()),
        )
        .primary_key(
            // Form composite primary key with enum
            sea_query::Index::create().col(Report::Id).col(Report::Version),
        )
        .to_owned()
}

Expected Behavior

Actual Behavior

Reproduces How Often

Versions

Additional Information

Pure-Peace added a commit to Pure-Peace/peace that referenced this issue Jan 5, 2023
Pure-Peace added a commit to Pure-Peace/peace that referenced this issue Jan 5, 2023
@billy1624 billy1624 self-assigned this Jan 21, 2023
@billy1624
Copy link
Member

billy1624 commented Jan 21, 2023

Hey @Pure-Peace, welcome to SeaQL. Thanks for the report!!

I just made a PR which provides a blanket implementation of TryFromU64 for all ActiveEnum

impl<T> TryFromU64 for T
where
    T: ActiveEnum,
{
    fn try_from_u64(_: u64) -> Result<Self, DbErr> {
        Err(DbErr::ConvertFromU64(
            "Fail to construct ActiveEnum from a u64, if your primary key consist of a ActiveEnum field, its auto increment should be set to false."
        ))
    }
}

@billy1624 billy1624 moved this from Triage to In Progress in SeaQL Dev Tracker Jan 21, 2023
@billy1624 billy1624 added this to the 0.11.x milestone Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants