Load multiple models with true eager loading #1227
-
After playing around with SQLBoiler a bit, I realized that I have the following models:
where the How can I achieve true eager loading and given a table A id, to load both the corresponding TableA and TableB entries with a single SQL query? I've already tried something like the following:
but it looks like this messes up the fields, because they have the same names. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You have to prefix the alias names correctly for bind to work. type IntermediateModel struct {
TableA TableA `boil:"tablea,bind"` // Note ",bind"
TableB TableB `boil:"tableb,bind"`
}
func retrieveModels(id string) IntermediateModel {
var response IntermediateModel
err := queries.Raw(`
SELECT
tablea.id AS "tablea.id",
tablea.value AS "tablea.value",
tableb.id AS "tablea.tableb_id",
tableb.id AS "tableb.id",
tableb.value AS "tableb.value"
FROM tablea
INNER JOIN tableb ON tableb.id = tablea.tableb_id
WHERE tablea.id=$1`, id).Bind(ctx, exec, &response)
return response
} I understand this is not very user friendly, but this is the best SQLBoiler can do right now. If your project is new, you can consider my other package Bob which can do what you want. In some ways, Bob is what I would make SQLBoiler if I could start from scratch. |
Beta Was this translation helpful? Give feedback.
-
@stephenafamo Thank you for your response. What we ended up doing to overcome the above was to create a helper method that expands the columns we need with appropriate aliases so that SQLBoiler can resolve naming clashes. So, the above example would become:
The We've noticed that such an array is generated (i.e. |
Beta Was this translation helpful? Give feedback.
You have to prefix the alias names correctly for bind to work.
I understand this is not very user friendly, but this…