Skip to content

Fix schema sync not discovering tables in non-default schemas#3016

Open
ouywm wants to merge 3 commits intoSeaQL:masterfrom
ouywm:fix/schema-sync-multi-schema-2952
Open

Fix schema sync not discovering tables in non-default schemas#3016
ouywm wants to merge 3 commits intoSeaQL:masterfrom
ouywm:fix/schema-sync-multi-schema-2952

Conversation

@ouywm
Copy link

@ouywm ouywm commented Mar 22, 2026

PR Info

New Features

  • N/A

Bug Fixes

  • sync() only ran SchemaDiscovery against CURRENT_SCHEMA() (typically public). Entities with #[sea_orm(schema_name = "sys")] were never discovered, so every sync
    attempted CREATE TABLE again, failing with relation "xxx" already exists. Root cause has two layers: (1) discovery was hardcoded to a single schema, (2) the table lookup
    compared full TableName (with schema qualifier) against discovered tables (without schema qualifier), so even if the table existed it could never match.

Breaking Changes

  • N/A. Fully backward compatible — entities without schema_name produce the same single-schema discovery as before.

Changes

  • Add schema_name: Option<String> field to EntitySchemaInfo, captured from EntityName::schema_name()
  • Replace DiscoveredSchema.tables: Vec<TableCreateStatement> with tables_by_schema: HashMap<String, Vec<TableCreateStatement>> keyed by schema name
  • Collect all unique target schemas from registered entities before discovery; run SchemaDiscovery::new_no_exec(schema) for each schema (Postgres & MySQL)
  • Add DiscoveredSchema::find_table() for schema-aware table lookup — resolves entity's effective schema (explicit or current_schema fallback), then compares bare table
    names within that bucket
  • SQLite unchanged (uses empty string as schema key)
  • Regenerated sea-orm-sync via make-sync.sh

@ouywm ouywm force-pushed the fix/schema-sync-multi-schema-2952 branch from 5a47cca to 798bc48 Compare March 22, 2026 11:26
@ouywm
Copy link
Author

ouywm commented Mar 22, 2026 via email

@ouywm ouywm force-pushed the fix/schema-sync-multi-schema-2952 branch from 798bc48 to da72401 Compare March 22, 2026 12:05
@Huliiiiii
Copy link
Member

Could you add a test for this?

}

let mut tables_by_schema = std::collections::HashMap::new();
let mut all_enums = Vec::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enums should be grouped by schema as well.

@Huliiiiii
Copy link
Member

Huliiiiii commented Mar 22, 2026

I noticed that the changes in sea-orm-sync and sea-orm are not aligned. I've updated sea-orm-sync. Could you rebase onto master and make sure the changes are consistent between sea-orm and sea-orm-sync?

@ouywm
Copy link
Author

ouywm commented Mar 22, 2026

Good call, thanks — I'll add a regression test for this and update the PR soon.

@ouywm ouywm force-pushed the fix/schema-sync-multi-schema-2952 branch from da72401 to 845aec0 Compare March 22, 2026 14:06
@ouywm ouywm force-pushed the fix/schema-sync-multi-schema-2952 branch from 845aec0 to 1e278de Compare March 22, 2026 14:57
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 this pull request may close these issues.

entity sync does not work with postgres when tables have schemas defined

2 participants