I've just started getting this error for multiple entities, but I'm having a nightmare pinning down how to fix it.
Last night, it happened after I added a 'has many' relationship to an existing entity. After 2 hours, I was able to solve it simply by reordering the order in which migrations were specified with db.Automigrate()`, but I'll admit theres a chance that's was an illusory fix.
Everything was fine until I just added another has many relationship to another entity and I'm getting the message again, but no amount of moving migrations around will fix it.
In both cases, the entity I added the has many relation to is NOT the one mentioned in the error message. (E.g. I added Orders[] to Customer, and the message I got was can't preload field Item for core.PurchaseOrderLine) - seemingly unrelated.
I should stress that it is DEFINITELY the addition of the has many field that causes the error - if I remove it, the error stops.
Any suggestions highly appreciated.
I got to the root source of this problem. Let me try to explain:
In both cases, the preload field that was failing to load was not directly inside the entity, but wrapped inside an anonymous struct, e.g.
type Line struct {
LineCommon
}
type LineCommon struct {
gorm.Model
ItemID uint
Item Item
}
This was causing the failure. Denesting fixes it:
type Line struct {
gorm.Model
ItemID uint
Item Item
}
Im not sure if thats intended behaviour - i guess not. The workaround is annoying, but not the end of the world for me.
It's not "anonymous" it's "embedded". You embed LineCommon into Line structure. Same behavior with GORM will be achieved by using gorm:"embedded" struct tag.
Just use this instead:
type Line struct {
LineCommon LineCommon
}
in this case you are not embedding LineCommon struct. This is normal, idiomatic, and expected result with both Go and GORM.
Using a named struct field wouldn't give me the functionality I want - I'll use the gorm:"embedded" tag. Thanks.
type Line struct {
LineCommon gorm:"embedded"
}
didn't work for me. I'm back to unnesting the fields.
Of course it didn't work, it's same as embedding. Read carefully.
I did read carefully. I'm not sure what advice you are giving me. I can't use a named field struct as I need to be able to access LineCommon's methods. I can't find any documentation for gorm:embeddedso I'm not sure what it does or where to use it.
The point was to explain you what is happening and how it works. This struct tag not documented, but it's easy to find in the code: https://github.com/jinzhu/gorm/blob/0fd395ab37aefd2d50854f0556a4311dccc6f45a/embedded_struct_test.go#L24
OK - lets try to simplify, as I think we are not understanding each other. Look at my second comment (Line/LineCommon). I have about 20 types, all structured like this - with the gorm.Model and other related fields embedded. Up until a few days ago, no problems at all. So from that, I undestand that it should 'just work', because, well, it does.
A few days ago, I added a has many relationship to one of those types, and I started getting preload errors ON ANOTHER type. I solved it by shifting around the order of the migrations. A few days later, some thing. The only way I've managed to completely solve it is by de-embedding (like the 3rd example in my comment).
I'm just trying to alert that there might be a subtle bug here, although I can completely understand that I haven't provided nearly enough information to track it down.
type Line struct {
LineCommon []LineCommon `gorm:"ForeignKey:ItemID"`
}
type LineCommon struct {
gorm.Model
ItemID uint
Item Item
}
This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you
Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog
Most helpful comment