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

sea-orm-codegen: Enum use statements getting generated multiple times #1699

Closed
lukasschlueter opened this issue Jun 11, 2023 · 1 comment · Fixed by #1953
Closed

sea-orm-codegen: Enum use statements getting generated multiple times #1699

lukasschlueter opened this issue Jun 11, 2023 · 1 comment · Fixed by #1953

Comments

@lukasschlueter
Copy link
Contributor

Description

When using PostgreSQL, a table that has two columns using the same enum causes the generated entity to have duplicate use statements, which cause a compilation error.

Steps to Reproduce

  1. Create a migration like this:
use sea_orm_migration::prelude::*;
use sea_orm_migration::prelude::extension::postgres::Type;
use sea_orm_migration::sea_orm::DbBackend;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        assert_eq!(manager.get_database_backend(), DbBackend::Postgres);
        manager.create_type(Type::create().as_enum(MyEnum::Table).values([MyEnum::A, MyEnum::B]).to_owned()).await?;

        manager
            .create_table(
                Table::create()
                    .table(MyTable::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(MyTable::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(MyTable::Col1).enumeration(MyEnum::Table, [MyEnum::A, MyEnum::B]).not_null())
                    .col(ColumnDef::new(MyTable::Col2).enumeration(MyEnum::Table, [MyEnum::A, MyEnum::B]).not_null())
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(MyTable::Table).to_owned())
            .await
    }
}

/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum MyTable {
    Table,
    Id,
    Col1,
    Col2,
}

#[derive(Iden)]
enum MyEnum {
    Table,
    A,
    B,
}
  1. Migrate: sea migrate
  2. Generate entities: sea generate entity -o src/entities
  3. Have a look at the generated file (in this case: src/entities/my_table.rs)

Expected Behavior

The generated file has only one import and compiles without errors.
Generated file should look like this:

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

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

/* ... */

Actual Behavior

Generated file has duplicate use statements and looks like this:

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

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

/* ... */

Reproduces How Often

Always

Workarounds

  1. Manually remove the duplicate use statement each time

  2. I've implemented a simple fix in 7cf3195

Note: I tried to fix this and manage to solve it for my project where the bug originally showed up.
However, I would have liked to add some kind of tests for that but I didn't really understand how. - https://github.com/SeaQL/sea-orm/blob/master/CONTRIBUTING.md did state how to run all the tests (which were successful for my commit) but not how the test system works and where to add my own tests.
I currently don't have the time to try to understand how tests work, so unfortunately, I don't think it would be much help if I opened a PR myself (or would it?).

So if someone wants to pick this up - feel free to use my changes, the main thing that's missing is some test.

Reproducible Example

Currently, it does not seem like I will have time to add a PR for that unfortunately.

Versions

sea-orm-cli: master (1431d80)
Postgres (14.5 - should not matter)
Windows 10 (should not matter)

@billy1624
Copy link
Member

Hey @lukasschlueter, nice catch and neat little patch! A PR would be welcomed! Could you please file a PR when you got time?

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