jet
jet copied to clipboard
Remove table name from columns alias
I have an older project where I would like to use Jet as a query generator. The project already contains models with db tags and binding via sqlx.
Jet by default generate queries with column aliases including table names, e.g. for a table called jet the code
stmt := SELECT(Jet.ID).FROM(Jet)
query, _ := stmt.Sql()
fmt.Printf("Query: %s\n", query)
prints
SELECT jet.id AS "jet.id" FROM jet;
I would like to have a setting where the above stmt prints SELECT jet.id AS "id" FROM jet; by default.
I know I can manually add aliases to the columns in the query but for porting an older project to Jet it gets tedious quickly.
This answer suggests creating an alias for the table but even defining a table alias with an empty string aliasedTable := Jet.AS("") does not remove the table name from the column alias.
This https://github.com/go-jet/jet/issues/26#issuecomment-559777571 suggests creating an alias for the table but even defining a table alias with an empty string aliasedTable := Jet.AS("") does not remove the table name from the column alias.
Empty alias string means no alias.
I don't see other way then alias each column manually:
stmt := SELECT(Jet.ID.AS("id")).FROM(Jet)
Although, far easier would be to just ditch sqlx.
Well the problem is not sqlx per se the problem is the hundreds of model structs that are already in the codebase that expect column name aliases without the table name, either due to existing db tag or because of default.
You can use jet with custom model types as well. Also those db aliases will be ignored by jet QRM.