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

Struct SelectFrom should include full table name #40

Closed
albertorestifo opened this issue Nov 19, 2019 · 4 comments
Closed

Struct SelectFrom should include full table name #40

albertorestifo opened this issue Nov 19, 2019 · 4 comments

Comments

@albertorestifo
Copy link

When making joins, it would be useful is the Struct added the table name to the fields or if it implemented a SelectFromAs method.

Right now, for example, there is no way of doing this:

var c Company
sb := companyStruct.SelectFrom("companies", &c)
sb.Join("company_balances", "company_balances.company_id = companies.company_id").
	Where(sb.E("balance_id", balanceID)).
	Limit(1)

This will fail as both the companies and company_balances table have a company_id field.

@albertorestifo
Copy link
Author

albertorestifo commented Nov 20, 2019

@huandu I'm willing to make a PR to help with this as it's important for our use-case.

Which approach do you prefer?

  1. Add a SelectFromAs to the Struct
  2. Always prefix the fields with the table name

I would personally go with 2, as it can be applied to all queries in the generator and it won't break existing queries

@huandu
Copy link
Owner

huandu commented Nov 21, 2019

Thanks for your suggestion, @albertorestifo. I'm thinking of how to design it these days. The ideal solution in my mind is similar to 2 except that the prefix is added only if JOIN is used in sb. It's not easy to implement it in this way without magic. That's why I'm still thinking of it.

@huandu
Copy link
Owner

huandu commented Nov 21, 2019

The commit fb2186e implements solution 2. I still cannot find a way to add prefix only if JOIN is used without ugly magic. Let's go with solution 2 before we have a perfect solution.

@huandu huandu closed this as completed in ccb767d Nov 21, 2019
@albertorestifo
Copy link
Author

This worked perfectly, many thanks for the speedy solution

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