go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

Remove table name from columns alias #263

Open dedalusj opened 10 months ago

dedalusj commented 10 months ago

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.

houten11 commented 10 months ago

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.

dedalusj commented 10 months ago

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.

go-jet commented 10 months ago

You can use jet with custom model types as well. Also those db aliases will be ignored by jet QRM.