problem
consider the following struct
struct A {
ID string `gorm:"type:varchar(255)"`
BID string `gorm:"type:varchar(255)"`
b B `gorm:"foreignkey:BID"`
}
struct B {
ID string `gorm:"type:varchar(255)"`
cs []*C `gorm:"foreignkey:BID"`
}
struct C {
ID string `gorm:"type:varchar(255)"`
BID string `gorm:"type:varchar(255)"`
}
When I using query to list A's records, preloadEverything function in gorm/fields.go will find out preloads for B and B.C; however, when gorm preloads B, it will preload C in preload function which is after query function, and preloadMap will get B and C as key, and then return. As key C != key B.C, so gorm will preload C again, and gets duplicated C's records for B.cs
confused
When I checked preloadEverything function, I am really confused for subpreload as sf.Name has also been put into toPreload function (which means subpreload will be done when gorm preload sf.Name).
So why need to preload subpreload?
Solution
I just preload sf.Name and remove subpreload and the problem of duplicated records is solved.
problem
consider the following struct
When I using query to list A's records,
preloadEverythingfunction ingorm/fields.gowill find out preloads forBandB.C; however, when gorm preloads B, it will preload C in preload function which is after query function, and preloadMap will getBandCas key, and then return. As keyC!= keyB.C, so gorm will preload C again, and gets duplicated C's records for B.csconfused
When I checked
preloadEverythingfunction, I am really confused forsubpreloadassf.Namehas also been put intotoPreloadfunction (which means subpreload will be done when gorm preloadsf.Name).So why need to preload
subpreload?Solution
I just preload
sf.Nameand removesubpreloadand the problem of duplicated records is solved.