-
-
Notifications
You must be signed in to change notification settings - Fork 607
feat: allow cross schema type/fk/enum generation #2745
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
base: master
Are you sure you want to change the base?
feat: allow cross schema type/fk/enum generation #2745
Conversation
"#; | ||
|
||
let enum_rows = sqlx::query(enum_query).fetch_all(&pool).await?; | ||
let mut all_enums: HashMap<String, Vec<String>> = HashMap::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this variable used for?
println!( | ||
"Discovered {} tables across {} schemas", | ||
all_tables.len(), | ||
schemas_to_discover.len() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I ask, are you doing vibe coding? This code is hard to understand and print too much logs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used Claude to clean up when I was done
// This allows types (like enums) defined in any schema to be discovered | ||
// PostgreSQL will search in order: target schema, then "$user", then public | ||
// See: https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH | ||
let sql = format!("SET search_path = '{schema}', \"$user\", public"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this addresses the issue... If the referenced type comes from another schema, this would still fail. Also, there will be conflicts between tables/types with the same name from different schemas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe my approach to testing locally is a bit naive, I tested with a custom schema and public schema and it generated the entities. I'll test again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the status_type to a new control_schema and it was generated under the public schema. I'll fix
schemas_to_discover.len() | ||
); | ||
|
||
let table_stmts = all_tables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, this will generate tables of all schemas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It finds all the tables and only generates the necessary ones. For example if there's a foreign key on another schema it will bring the relation in, same enums and types in other schemas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is your code, I didn't see you filtering out tables that are not part of the target schema.
let mut all_tables = Vec::new();
for discover_schema in schemas_to_discover.iter() {
println!("Discovering tables in schema: {}", discover_schema);
let discovery = SchemaDiscovery::new(pool.clone(), discover_schema);
let discovered = discovery.discover().await?;
all_tables.extend(discovered.tables);
}
let table_stmts = all_tables
PR Info
New Features
Bug Fixes
Fixed entity generation failure with cross-schema type references: Previously, running
sea-orm-cli generate entity --database-schema custom_schema
would fail with "type does not exist" errors when tables incustom_schema
referenced enums or had foreign keys to tables in other schemas (e.g.,public
).Root cause: sea-schema's discovery was limited to the target schema only, so it couldn't resolve cross-schema type references. This was fixed by first loading all schema types/enums etc before proceeding with generation.
Breaking Changes
Changes
sea-orm-cli/src/commands/generate.rs
:search_path
configuration to include"$user"
andpublic
schemasTesting
Manual testing performed with the reproduction case from #2584: