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

Generate entities: RcOrArc<ColumnDef> passed for ColumnType::Array #2409

Open
pranc1ngpegasus opened this issue Nov 7, 2024 · 1 comment

Comments

@pranc1ngpegasus
Copy link

pranc1ngpegasus commented Nov 7, 2024

Description

I ran sea-orm-cli generate entity and generated Rust code does not compile:

error[E0308]: mismatched types
   --> entities/src/gen/foo.rs:<line>:<col>
    |
69  |             Self::FooBar => ColumnType::Array(RcOrArc::new(BarBaz::db_type())).def(),
    |                                              ------------ ^^^^^^^^^^^^^^^^^^^^^ expected `ColumnType`, found `ColumnDef`
    |                                              |
    |                                              arguments to this function are incorrect
    |

Steps to Reproduce

  1. Have a enum of any type
  2. Have a table with column of type enum[]
  3. Generate entities from the table
  4. Try to compile the generated Rust code

Expected Behavior

Compiles

Actual Behavior

Not compiles

Reproduces How Often

Consistent

Workarounds

Edit generated code like this:

-use sea_orm::entity::prelude::*;
+use sea_orm::{entity::prelude::*, sea_query::ValueType};

 #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
@@ -66,7 +66,7 @@ impl ColumnTrait for Column {
-            Self::FooBar => ColumnType::Array(RcOrArc::new(BarBaz::db_type())).def(),
+            Self::FooBar => ColumnType::Array(RcOrArc::new(BarBaz::column_type())).def(),

Of course it will be overwritten when we re-generate Rust code :(

Reproducible Example

migration file:

#[derive(Iden)]
pub enum BarBaz {
  #[iden = "bar_baz"]
  Enum,
  #[iden = "variant_1"]
  Variant1,
}

manager
    .create_type(
        extension::postgres::Type::create()
            .as_enum(BarBaz::Enum)
            .values([
                BarBaz::Variant1,
            ])
            .to_owned(),
    )
    .await?;

manager
    .create_table(
        Table::create()
            .table(Foo::Table)
            .if_not_exists()
            .col(
                ColumnDef::new(Foo::FooBar)
                    .custom(Alias::new("bar_baz[]"))
                    .not_null(),
            )
            .to_owned(),
    )
    .await?;

Versions

$ cargo tree | grep sea-orm
│   ├── sea-orm v1.1.1
│   │   ├── sea-orm-macros v1.1.1 (proc-macro)
│   ├── sea-orm v1.1.1 (*)
│   ├── sea-orm v1.1.1 (*)
│   │   └── sea-orm-migration v1.1.1
│   │       ├── sea-orm v1.1.1 (*)
│   │       ├── sea-orm-cli v1.1.1
│   │   ├── sea-orm v1.1.1 (*)
├── sea-orm v1.1.1 (*)
@pranc1ngpegasus pranc1ngpegasus changed the title Generate entities: SeaRc<ColumnDef> passed for ColumnType::Array Generate entities: RcOrArc<ColumnDef> passed for ColumnType::Array Nov 7, 2024
@pranc1ngpegasus
Copy link
Author

When we run sea-orm-cli generate entity, sea-orm-codegen will generates ColumnDef code for array of enum.
Then those code will run.

ColumnType::Array(column_type) => {
let column_type = write_col_def(column_type);
quote! { ColumnType::Array(RcOrArc::new(#column_type)) }
}

ColumnType::Enum { name, .. } => {
let enum_ident = format_ident!("{}", name.to_string().to_upper_camel_case());
quote! { #enum_ident::db_type() }
}

For Array type, those code will generated.

ColumnType::Array(RcOrArc::new(/*column definition for enum will appear here*/))

For Enum type, those code will generated.

BarBaz::db_type()

ColumnType::Array requires RcOrArc<ColumnType> for first argument, but BarBaz::db_type() returns ColumnDef.
I think that is the matter.

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

No branches or pull requests

1 participant