Skip to content

duplicated preloads List duplicated records in mysql with gorm #200

@Chaunhewie

Description

@Chaunhewie

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions