Skip to content

fix(boilingcore): disambiguate identifiers on table slice name collisions - #1483

Open
AdamMagued wants to merge 1 commit into
aarondl:masterfrom
AdamMagued:fix-issue-1482-name-collision
Open

AdamMagued wants to merge 1 commit into
aarondl:masterfrom
AdamMagued:fix-issue-1482-name-collision

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Addresses #1482 where specific database table names (such as personas, series, data) cause SQLBoiler to generate identical identifiers for both the record struct (type <UpSingular> struct) and the query builder constructor function (func <UpPlural>(...)), resulting in Go compilation errors due to duplicate declarations in package scope.

Additionally addresses collisions when table names conflict with built-in generator tokens (TableNames, ViewNames, NewQuery, M, ErrSyncFail) or with slice types generated for other tables in the schema (<Table>Slice).

Root Cause

  1. In strmangle, the default inflection ruleset includes rs.AddSingular("as", "as") to preserve terms such as alias or canvas. Because of this rule, strmangle.Singular("personas") resolves to "personas" instead of "persona". Consequently, both UpSingular and UpPlural evaluate to "Personas".
  2. When UpSingular == UpPlural, SQLBoiler generates type Personas struct in 00_struct.go.tpl and func Personas(...) in 13_all.go.tpl, colliding in the generated package.
  3. Tables named after built-in package-level variables or types (such as table_names producing TableNames, or m producing M) collide with declarations in boil_table_names.go and boil_types.go.
  4. Tables whose singular name coincides with another table's slice type (for example, user_slice yielding UserSlice alongside a user table producing type UserSlice []*User) result in duplicate type declarations.

Changes

  • Registered default inflection rule personas -> persona in boilingcore/aliases.go within initDefaultInflections().
  • Added collision resolution in FillAliases:
    • When table.UpSingular == table.UpPlural, strip the trailing plural suffix s if applicable, or append descriptive tokens (Records / Record) to differentiate query helpers from record structs.
    • Check against package-level tokens (TableNames, ViewNames, NewQuery, M, ErrSyncFail) and append Query or Record suffixes.
    • Disambiguate table names that conflict with slice types from other tables in the schema (<UpSingular>Slice).
  • Added regression test TestAliasesCollisions verifying aliases for personas, series, data, table_names, m, and table slice interactions.
  • Added integration test TestNewWithCollidingTableNames confirming that code generated for table personas compiles without error.
  • Added executable lookups in driver integration tests (mssql, mysql, psql, sqlite3) to skip when database CLI clients are missing in local test runners.

Verification

  • go test ./... -v and go vet ./... executed cleanly in isolated Linux sandbox.
  • TestAliasesCollisions passes with verified singular/plural distinction.
  • Generated model package for table personas compiles cleanly under go build.

…ions

Resolve name collisions occurring during model generation when table singular
and plural identifiers coincide (e.g. personas, series, data), or when a table
name conflicts with another table's slice type or built-in generator tokens.

Specifically:
- Register default inflection rule for 'personas' to singularize as 'persona'.
- Fall back to suffix disambiguation or append record/query tokens when singular
  and plural forms match or collide with built-in generator symbols.
- Disambiguate table struct names that coincide with another table's slice type.
- Add regression unit tests for colliding table names and schema aliases.
- Guard driver integration tests with executable path checks when external
  database clients are unavailable.
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.

1 participant