SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.57k stars 461 forks source link

Entity Generation Fails to Skip Ignored Tables #2089

Open cxgreat2014 opened 5 months ago

cxgreat2014 commented 5 months ago

Description

Encountering an issue (refer to GitHub issue #123), I aimed to exclude specific tables during entity generation. However, despite utilizing the --ignore-tables flag, the entity generation process failed without skipping the specified tables.

Steps to Reproduce

Execute the following command:

sea-orm-cli generate entity --ignore-tables django_apscheduler_djangojob,django_apscheduler_djangojobexecution -v

Check the log for details:

2024-01-30T15:32:35.334322Z DEBUG sqlx::query: PRAGMA foreign_key_list('django_apscheduler_djangojob') - elapsed=15.2µs
2024-01-30T15:32:35.334558Z DEBUG sqlx::query: PRAGMA table_info('django_apscheduler_djangojob') - elapsed=86.6µs
2024-01-30T15:32:35.334833Z DEBUG sqlx::query: PRAGMA index_list('django_apscheduler_djangojob') - elapsed=84.5µs
2024-01-30T15:32:35.335242Z DEBUG sqlx::query: SELECT ? FROM "sqlite_master" ... - elapsed=108.5µs
2024-01-30T15:32:35.335519Z DEBUG sqlx::query: PRAGMA foreign_key_list('django_apscheduler_djangojobexecution') - elapsed=80.1µs
2024-01-30T15:32:35.335822Z DEBUG sqlx::query: PRAGMA table_info('django_apscheduler_djangojobexecution') - elapsed=104.8µs
thread 'main' panicked at C:\Users\rustdev\.cargo\registry\src\index.crates.io-6f17d22bba15001f\sea-schema-0.14.2\src\sqlite\def\types.rs:62:42:
index out of bounds: the len is 1 but the index is 1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected Behavior

Actual Behavior

Reproduces How Often

Workarounds

Reproducible Example

Versions

cxgreat2014 commented 5 months ago

Fix method:

sea-schema/src/sqlite/discovery.rs:23:

    pub async fn discover(&self, ignore_tables: Vec<String>) -> DiscoveryResult<Schema> {
        let get_tables = SelectStatement::new()
            .column(Alias::new("name"))
            .from(SqliteMaster)
            .and_where(Expr::col(Alias::new("type")).eq("table"))
            .and_where(Expr::col(Alias::new("name")).ne("sqlite_sequence"))
            .and_where(Expr::col(Alias::new("name")).is_not_in(ignore_tables))
            .to_owned();

sea-orm-cli/src/commands/generate.rs:78

            let ignore_tables_clone = ignore_tables.clone();
            let filter_skip_tables = |table: &String| -> bool { !ignore_tables_clone.contains(table) };

sea-orm-cli/src/commands/generate.rs:139

let schema = schema_discovery.discover(ignore_tables).await?;
tyt2y3 commented 4 months ago

Thank you for your report. Seems like you have a proposed fix. Can you open a PR?

cxgreat2014 commented 4 months ago

My fixes are superficial and not fully tested, and in the case of non-sqlite database branches there is no compatibility processing. The current code logic changes will significantly lower the current code quality. I hope that some sea-orm members can understand the problem more quickly and know a dirty hack fix based on the information I provided above. Then make a good design and submit clean fix code and test files to solve this problem,(which means a lot of work and takes a lot of time and I can't do it at present), so I hope there are more kind people like you to do this PR