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

How to access inner join data? #536

Closed
kallaway opened this issue May 22, 2019 · 2 comments
Closed

How to access inner join data? #536

kallaway opened this issue May 22, 2019 · 2 comments

Comments

@kallaway
Copy link

@kallaway kallaway commented May 22, 2019

Hello, I am sorry for a potentially silly question, but I've been stuck on this for a while now.

What is the correct way of accessing data that we 'joined' after we got the results back?

Here is an example from the docs:
// Ultra complex query users, err := models.Users( Select("id", "name"), InnerJoin("credit_cards c on c.user_id = users.id"), Where("age > ?", 30), AndIn("c.kind in ?", "visa", "mastercard"), Or("email like ?",%aol.com%), GroupBy("id", "name"), Having("count(c.id) > ?", 2), Limit(5), Offset(6), ).All(ctx, db)

My question is, are we able to access users[0].c.Kind or similar data from the credit_cards table that we joined - after the fact? I see the example of:
AndIn("c.kind in ?", "visa", "mastercard")
where we access the data while still in the 'query building process', but can we somehow access that after the call has returned (and given values to 'users' and 'err') ?

If there is no way to do that, what would be an alternative approach? Making the requests to get the additional data as part of a bigger transaction (more than one call)?

I apologize for this basic question, but I couldn't find an answer to this so far.

@aarondl

This comment has been minimized.

Copy link
Member

@aarondl aarondl commented May 23, 2019

It's actually a little bit of a funny way that we do inner joins. I don't love it but it does work.

Take a look at the docs here:
https://github.com/volatiletech/sqlboiler#binding

It shows how Bind() can be used to bind to a struct that contains more than one model. It's not pretty because in order for bind to understand which column belongs to what struct inside the struct they must be named with the . to allow Bind to figure it out. (Unique names are also a concern).

If you don't like that approach, you can just create a new struct with all the fields you want from the inner join and bind with that:

type myJoinStruct struct {
  PilotID int `boil:"pilot_id"`
  JetID   int `boil:"jet_id"`
  ...
}

qm.Select("pilot.id as pilot_id, jet.id as jet_id ...")

The last option of course is to just not use sqlboiler at all for this operation and use scan to ignore names and go based on ordinal position of the returned columns alone (this circumvents the need to as all your columns which is arguably the most annoying part about the inner join support in sqlboiler).

Hopefully that answers the question!

@aarondl aarondl closed this May 23, 2019
@kallaway

This comment has been minimized.

Copy link
Author

@kallaway kallaway commented May 23, 2019

@aarondl Thank you for your response! This definitely helps! :)

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.