0
votes
type MainStruct struct {
Defaults
Foo string
Bar string
Baz int64
Struct1  MyStruct1 `gorm:"foreignkey:MainStructID"`
}
type MyStruct1 struct {
    Defaults
    MainStructID int64                  `json:"-"`       
    ID1          int64                  `json:"-"` 
    ID2         int64                   `json:"-"` 
    MyDefault1     MyStruct2 `gorm: "foriegnkey:ID;association_foreignkey:ID1"`
    MyDefault2    MyStruct2 `gorm: "foriegnkey:ID;association_foreignkey:ID2"`
}
type MyStruct2 struct {
    Defaults
    Field1              string
    Field2           string
    Field3          string
}

Given the above Structs I want to do something along the lines of

baseVersion MainStruct
db.Where("ID = ?", myInputID).Preload("Struct1").
        Preload("Struct1.MyDefault1").
        Preload("Struct1.MyDefault2").
        First(&baseVersion)

The issue I am having currently is that I am not getting back the data from struct2 at all. I think it is an issue with the gorm annotation in Struct1 creating the association ... but I am not sure what is wrong.

No matter how I have tried to get the Preload od MyDefault1 and MyDefault2 to come in. I always wind up with an error that it cannot preload. My guess is that the gorm annotation is not completely correct ... but it seems to be following the pattern so I am not sure what to look at now.

1

1 Answers

0
votes
  • There is a spelling mistake in MyStruct1: foriegnkeyforeignkey.
  • There is an extra space in MyStruct1's tag value.

These reasons make Gorm unable to analyze MyStruct1's relationship.

type MyStruct1 struct {
    Model
    MainStructID int64     `json:"-"`
    ID1          int64     `json:"-"`
    ID2          int64     `json:"-"`
    MyDefault1   MyStruct2 `gorm:"foreignkey:ID;association_foreignkey:ID1"`
    MyDefault2   MyStruct2 `gorm:"foreignkey:ID;association_foreignkey:ID2"`
}