[Solved] Aliasing a column

Is there any way to alias a column on a select? I mean an actual alias on the final query.. In starting to test out the new views support (amazing btw), I noticed a "problem".. Normally if we have colliding colum names, like for instance: users.name and posts.name, we would alias them as user_name and post_name, for example, but currently drizzle queries them as "users"."name" and "posts"."name" In the view, which, at least in SQLite, creates a view with two "name" columns (returned as name and name:1) That creates issues when filtering and joining data.. Maybe supporting the .as() function on columns for selects in general could be useful? Instead of only supporting it on sql``?
4 Replies
Natan Parmigiano
Natan ParmigianoOP3mo ago
I wrote a little generic function that does exactly what i want, for anyone interested:
const aliasedColumn = <T extends SQLiteColumn>(column: T, alias: string) => {
return sql<GetColumnData<T>>`${column}`.as(alias);
};
const aliasedColumn = <T extends SQLiteColumn>(column: T, alias: string) => {
return sql<GetColumnData<T>>`${column}`.as(alias);
};
Natan Parmigiano
Natan ParmigianoOP3mo ago
No description
Natan Parmigiano
Natan ParmigianoOP3mo ago
May be used as such in views! It generates views with aliased columns:
CREATE VIEW `test` AS select "users"."id", "sessions"."id" as "session_id" from "users" left join "sessions" on "users"."id" = "sessions"."user_id";
CREATE VIEW `test` AS select "users"."id", "sessions"."id" as "session_id" from "users" left join "sessions" on "users"."id" = "sessions"."user_id";
Natan Parmigiano
Natan ParmigianoOP3mo ago
And keeps the results properly typed based on the source column:
No description
Want results from more Discord servers?
Add your server