entc/gen: bake Schema into generated code and allow same table names across schemas#4485
Open
dannyota wants to merge 1 commit intoent:masterfrom
Open
entc/gen: bake Schema into generated code and allow same table names across schemas#4485dannyota wants to merge 1 commit intoent:masterfrom
dannyota wants to merge 1 commit intoent:masterfrom
Conversation
…across schemas When two PostgreSQL schemas have tables with the same name (e.g., global.templates and lead.templates), code generation previously failed because Tables() rejected same table names across schemas and the template generated duplicate Go identifiers. This change: - Uses schema-qualified keys in the tables map to allow same table names in different schemas - Adds computeTableIdents() to generate unique Go identifiers: uses pascal(name) by default, falls back to pascal(schema)+"_"+pascal(name) on collision - Adds TableIdent() method on Graph, used by the schema template - Emits the Schema field in generated migrate/schema.go table structs - Fixes CopyTables to preserve Schema, Comment, View, Pos fields and use schema-qualified keys for FK ref lookups Fixes ent#2330
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2330
When two PostgreSQL schemas have tables with the same name (e.g.,
global.templatesandlead.templates), code generation fails becauseTables()rejects same table names across schemas and the template generates duplicate Go identifiers.Changes
entc/gen/graph.go"schema.table") in the tables map to allow same table names in different schemasnodeTmap (*Type → *schema.Table) for FK lookups instead of bare table name lookupscomputeTableIdents()to generate unique Go identifiers:pascal(name)by default,pascal(schema) + "_" + pascal(name)on collisionTableIdent(*schema.Table) stringmethod onGraph, used by the schema templateentc/gen/template/migrate/schema.tmplpascal $t.Name/pascal $fk.RefTable.Nameusages with$.TableIdentSchemafield in generated table structs when setdialect/sql/schema/schema.goCopyTablesto preserveSchema,Comment,View,Posfields (previously silently dropped)byNamemap and FK ref lookupBackward Compatibility
Schemafield emitted, identifiers unchanged. Generated code identical to before.UsersTable).Global_TemplatesTable).Table.SchemaoverridesCopyTablesSchemafield (was silently dropped).Testing
TestMultiSchemaSameTableName,TestSingleSchemaTableIdent,TestDuplicateTableNameSameSchema,TestCopyTablesMultiSchemamultischema/ent/migrate/schema.gowith Schema baked in, verified against MySQL 8