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

duplicated preloads List duplicated records in mysql with gorm #200

Open
Chaunhewie opened this issue Sep 8, 2020 · 0 comments
Open

duplicated preloads List duplicated records in mysql with gorm #200

Chaunhewie opened this issue Sep 8, 2020 · 0 comments

Comments

@Chaunhewie
Copy link

Chaunhewie commented Sep 8, 2020

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.

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

No branches or pull requests

1 participant