volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.66k stars 539 forks source link

Generation redeclares variables & aliases not applying #1401

Closed kelzenberg closed 1 month ago

kelzenberg commented 1 month ago

Generated model files redeclare variables from other generated model files because the table name finishes with the word "column". Then tests will fail with:

models/BmElementColumn.go:524:6: BmElementColumns redeclared in this block
    models/BmElement.go:172:5: other declaration of BmElementColumns

More info below.

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v4.16.2

What is your database and version (eg. Postgresql 10)

Microsoft SQL Server 16.0.1000

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

sqlboiler mssql go test ./models

If this happened at runtime what code produced the issue? (if not applicable leave blank)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

create table BmElement
(
    UUID  uniqueidentifier not null primary key,
    -- other attributes
)

create table BmElementColumn
(
    UUID  uniqueidentifier not null primary key,
    -- other attributes
)

Further information. What did you do, what did you expect?

sqlboiler.toml

pkgname = "models"
output = "models"
wipe = true
add-enum-types = true

[mssql]
dbname = "..."
host = "..."
port = 0
user = "..."
pass = "..."
schema = "dbo"
sslmode = "disable"

Among other tables in the mssql database, I have two tables BmElement and BmElementColumn (and I can't edit the schema).

As you can see the table naming interferes with the generated suffixes of table functions and variables. I tried to use aliases and inflection (see comment below).

kelzenberg commented 1 month ago

I tried aliasing the problematic table but I see no change in the generated models.

[aliases.tables]
BmElementColumn = "BmElementColumnAlias"

and

[aliases.tables.BmElementColumn]
up_plural = "BmElementColumnsA"
up_singular = "BmElementColumn"
down_plural = "bmElementColumnsA"
down_singular = "bmElementColumn"

Then I tried changing the way plural wording is created, also no change.

[inflections.plural_exact]
column = "columnsAll"

The TableNames struct in boil_table_names.go also continues to hold the BmElementColumn name. Both aliases and inflections do not change anything in the generated output. It also does not appear in the --debug output.

Unfortunately the documentation is not very clear in respect of how MSSQL table names are affected. What am I missing here?

kelzenberg commented 1 month ago

Adding this to the config file:

[aliases.tables.BmElementColumn]
up_plural = "BmElementColumnsFoo"
up_singular = "BmElementColumnFoo"
down_plural = "bmElementColumnsFoo"
down_singular = "bmElementColumnFoo"

Lists this in the JSON debug output:

"config": {
  "aliases": {
    "tables": {
        "BmElementColumn": {
          "up_plural": "BmElementColumns",
          "up_singular": "BmElementColumn",
          "down_plural": "bmElementColumns",
          "down_singular": "bmElementColumn",
          "columns": [...],
        }
        ...
        "bmelementcolumn": {
          "up_plural": "BmElementColumnsFoo",
          "up_singular": "BmElementColumnFoo",
          "down_plural": "bmElementColumnsFoo",
          "down_singular": "bmElementColumnFoo"
        }
    }
  }
}

Clearly the case-sensitivity is the culprit here. The part with

challenged by viper lowercasing all of your keys

makes sense now. The proposed solution...

[[aliases.tables]]
name = "BmElementColumn"
up_plural = "BmElementColumnsFoo"
up_singular = "BmElementColumnFoo"
down_plural = "bmElementColumnsFoo"
down_singular = "bmElementColumnFoo"

...was initially problematic but seems to work for now. Issue can be closed. What a journey...