Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Column identifier from aliased table #203

Closed
1 of 3 tasks
deitrix opened this issue Mar 4, 2020 · 2 comments
Closed
1 of 3 tasks

[Question] Column identifier from aliased table #203

deitrix opened this issue Mar 4, 2020 · 2 comments
Assignees
Milestone

Comments

@deitrix
Copy link

deitrix commented Mar 4, 2020

Hi Doug, quick question regarding table aliases. I'm not sure if this is something already built into the API, or if I'm doing it wrong.

I can't seem to figure out a way to generate the following query:

SELECT u.id, u.name, u.created_at, u.updated_at
FROM company_auth.users AS u

I would expect the code to look something along the lines of:

dialect := goqu.Dialect("mysql")

// Schema definitions.
authSchema := goqu.S("company_auth")

// Table definitions
usersTable := authSchema.Table("users")

u := usersTable.As("u")

query := dialect.From(u).Select(
	u.Col("id"),
	u.Col("name"),
	u.Col("created_at"),
	u.Col("updated_at"),
)

But, this does not compile.

I've noticed that I can retrieve an IdentifierExpression from an AliasedExpression by calling GetAs(), and while this does compile, it doesn't yield the desired query:

query := dialect.From(u).Select(
	u.GetAs().Col("id"),
	u.GetAs().Col("name"),
	u.GetAs().Col("created_at"),
	u.GetAs().Col("updated_at"),
)

generated query:

SELECT id, name, created_at, updated_at
FROM company_auth.users AS u

Dialect

  • postgres
  • mysql
  • sqlite3
@deitrix deitrix changed the title [QUESTION] Column identifier from aliased table. [QUESTION] Column identifier from aliased table Mar 4, 2020
@deitrix deitrix changed the title [QUESTION] Column identifier from aliased table [Question] Column identifier from aliased table Mar 4, 2020
@doug-martin
Copy link
Owner

Hi @deitrix sorry this has taken me a while to get back to you.

Yeah I'll need to take some time to create a fix for this.

While not ideal a work around would be

dialect := goqu.Dialect("mysql")

// Schema definitions.
authSchema := goqu.S("company_auth")

// Table definitions
usersTable := authSchema.Table("users")

u := goqu.T("u")

sql, _, _ = dialect.From(usersTable.As("u")).Select(
	u.Col("id"),
	u.Col("name"),
	u.Col("created_at"),
	u.Col("updated_at"),
).ToSQL()
fmt.Println(sql)

Output:

SELECT `u`.`id`, `u`.`name`, `u`.`created_at`, `u`.`updated_at` FROM `company_auth`.`users` AS `u`

@doug-martin doug-martin added this to the v9.7.1 milestone Mar 17, 2020
doug-martin added a commit that referenced this issue Mar 20, 2020
* [ADDED] Support for getting column identifiers from AliasExpressions. #203
@doug-martin doug-martin mentioned this issue Mar 20, 2020
doug-martin added a commit that referenced this issue Mar 20, 2020
* [ADDED] Support for getting column identifiers from AliasExpressions. #203
@doug-martin doug-martin mentioned this issue Mar 20, 2020
doug-martin added a commit that referenced this issue Mar 20, 2020
* [ADDED] Support for getting column identifiers from AliasExpressions. #203
@doug-martin
Copy link
Owner

Released in v9.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants