volatiletech / sqlboiler

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

Configuring aliases for relationships for sqlite is not working #1333

Closed leonlaser closed 9 months ago

leonlaser commented 9 months ago

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

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

SQLBoiler v4.15.0

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

sqlite3

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

sqlboiler sqlite3

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?

I included only the relevant part for the aliases, but I can also post the whole thing, if necessary.

"aliases": {
  "tables": {
    "table_main": {
      ...
    },
    "table_relation": {
      ...
      "relationships": {
        "FK_0": {
          "local": "TableRelations",
          "foreign": "TableMain"
        },
        "fk_0": {
          "local": "AliasLocal",
          "foreign": "AliasRemote"
        }
      }
    }
  }
},

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

CREATE TABLE table_main (
    id TEXT PRIMARY KEY
);

CREATE TABLE table_relation (
    id TEXT PRIMARY KEY,
    table_main_id TEXT REFERENCES table_main(id)
);

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

I am configuring aliases for a relationship:

[sqlite3]
dbname = "foreignkeys.sqlite3"

[aliases.tables.table_relation.relationships.FK_0]
local = "AliasLocal"
foreign = "AliasRemote"

I expected my configuration to work, but neither local nor foreign are changed in the generated sources.

The sqlite3 driver is creating names for each foreign key with an uppercase prefix FK_, but viper has no support for case sensitive keys, therefore my aliases are is not applied.

leonlaser commented 9 months ago

I just read about the alternate syntax in the docs and using name to overcome this problem, but maybe it would still be nice to align the case of the foreign key prefix to vipers current behavior?

stephenafamo commented 9 months ago

Sorry about this, I have also been bitten by Viper's lack of case sensitivity.

but maybe it would still be nice to align the case of the foreign key prefix to vipers current behavior

I don't understand exactly what you mean.

As far as I know there is no way to change viper's behaviour, the user can either use the alternative syntax, or we have to entirely replace Viper is SQLBoiler.

leonlaser commented 9 months ago

@stephenafamo I mean changing the generated names of foreign keys to lowercase in this line: https://github.com/volatiletech/sqlboiler/blob/71f11953fe83273d17a7ff0436e79b1341f0286d/drivers/sqlboiler-sqlite3/driver/sqlite3.go#L486

stephenafamo commented 9 months ago

Okay, that makes sense. Kindly send in a PR and I'll be happy to merge

stephenafamo commented 9 months ago

Although.... I'm not sure if this should be considered a breaking change since it may break existing configuration.

leonlaser commented 8 months ago

Yes, I agree. Viper has an open PR to enable case sensitivity by configuration. It might be better to wait for viper to offer a solution.