You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
problem
consider the following struct
When I using query to list A's records,
preloadEverything
function ingorm/fields.go
will find out preloads forB
andB.C
; however, when gorm preloads B, it will preload C in preload function which is after query function, and preloadMap will getB
andC
as 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
preloadEverything
function, I am really confused forsubpreload
assf.Name
has also been put intotoPreload
function (which means subpreload will be done when gorm preloadsf.Name
).So why need to preload
subpreload
?Solution
I just preload
sf.Name
and removesubpreload
and the problem of duplicated records is solved.The text was updated successfully, but these errors were encountered: