Hey,
is it possible to scan associations, because I tried but it didn't work so far:
var events []Event
self.DAO.DB.Raw("SELECT"+
" e.ID, e.title, e.description,"+
" d.ID, d.date, d.start_time, d.end_time,"+
" d.event_id"+
" FROM events e"+
" LEFT JOIN dates d ON e.ID = d.event_id").
Scan(&events)
For the following two example structs:
type Event struct {
ID int
Title string
Description string
Dates []Date
}
type Date struct {
ID int
Date time.Time
StartTime time.Time
EndTime time.Time
EventID int
}
It is not populating the Dates property, so it stays nil.
Comment From: ljesus
I have the same issue. I've searched and it seems that gorm does not support scanning associations on joins. Can anyone confirm? The way I've bypassed this issue is by scanning the values myself row by row.
Comment From: jinzhu
Hi @codingrogue
Yes, doesn't support Scan association, maybe you want to use Preload here?
Comment From: tom10271
How to use Preload? I have tried
rows, err := pDb.
Model(&entity.User{}).
Preload(`UserProfiles`).
Order(`id ASC`).
Rows()
but it is not working. db.ScanRow only populate user object except UserProfiles field
Comment From: shackra
Can confirm, Preload does nothing when using Rows or in ScanRows
Please re-open this
Comment From: petrogko
Has anyone found a solution for this? Im experiencing something similar.
Comment From: egsk
Came here for a same reason - Preload does not loading associations. Please, let us know if there any additional info.
Currently I'm using this temporary solution:
sql := "SELECT table.id ...
then I getting result as array (http://gorm.io/docs/sql_builder.html) and then just
db.Where("id in (?)", ids).Preload("Column").Find(&models);
It takes one more SQL query but still better than loading associations for each entity
Comment From: paultag
Same issue; this appears to be valid, Preload isn't doing anything with Rows as far as I can see, this bug should likely be re-opened
Comment From: m430
Same issue. I hope to support this feature. @jinzhu
Comment From: ali-gitinejad
yes we call result by sp and use gorm lib to populate data with scan and preload enable. but is doesnt work . it's very good solution which gorm support preload with scan plz add this feature