How can I access nested model fields for Preload chaining?

The example mentioned on GORM.io works fine but not when using GORM GEN Nested Model fields are not accessible

The document you expected this should be explained

https://github.com/go-gorm/gen#nested-preloading This works fine in GORM but not GORM-GEN

https://gorm.io/docs/preload.html#Nested-Preloading This works fine in GORM but not GORM-GEN

Expected answer

A way to preload nested association Fields

Let me explain my situation with renamed examples

type Parent struct {
    ID            int32   `gorm:"column:id;type:int;primaryKey;autoIncrement:false" json:"id"`
    Name          string `gorm:"column:name;type:varchar(64)" json:"name"`
    Children      []Child `gorm:"foreignKey:ParentID" json:"children"`
    Hobbies       []Hobby `gorm:"foreignKey:ParentID" json:"hobbies"`
}

type Child struct {
    ID                int32                `gorm:"column:id;type:int;primaryKey;autoIncrement:true" json:"id"`
    ParentID          *int32               `gorm:"column:parent_id;type:int;" json:"parent_id"`
    GrandSons         []GrandSon           `gorm:"foreignKey:ChildID" json:"grand_ss"`
    GrandDaughters    []GrandDaughter      `gorm:"foreignKey:ChildID" json:"grand_ds"`
}

type GrandSon struct {
    ID        int32 `gorm:"column:id;type:int;primaryKey;autoIncrement:false;" json:"id"`
    Name          string `gorm:"column:name;type:varchar(64)" json:"name"`
    ParentID int32  `gorm:"column:ChildID;type:int;primaryKey;autoIncrement:false" json:"child_id"`
}

type GrandDaughter struct {
    ID        int32 `gorm:"column:id;type:int;primaryKey;autoIncrement:false;" json:"id"`
    Name          string `gorm:"column:name;type:varchar(64)" json:"name"`
    ParentID  int32  `gorm:"column:ChildID;type:int;primaryKey;autoIncrement:false" json:"child_id"`
}

type Hobby struct {
    ID        int32 `gorm:"column:id;type:int;primaryKey;autoIncrement:false;" json:"id"`
    Name      string `gorm:"column:name;type:varchar(64)" json:"name"`
}

conn := db.GetDB()
var tree 
conn.Where("id= ?",id).
WithContext(c).
Preload(clause.Associations).
Preload("Children.GrandSons").
Preload("Children.GrandDaughters").
First(&tree)

This will give me the below result which is as expected

{
    "id":1
    "name":"john"
    "grand_ss":[
        {
            "id":1,
            "name":"John"
        }
    ],
    "grand_ds":[
        {
            "id":1,
            "name":"Jane"
        }
    ],
    "hobbies":[
        {
            "id":1,
            "name":"running"
        }
    ]
}

I assume GORM GEN equivalent would be - OPTION 1 id:= 1 t := query.Use(db.GetDB()).Parent tree, err := t.Where(t.ID.Eq(id)). Preload(field.Association). First() This only expands the first level of Associations { "id":1 "name":"john" "grand_ss":null, "grand_ds":null, "hobbies":[ { "id":1, "name":"running" } ] }

  • OPTION 2 id:= 1 t := query.Use(db.GetDB()).Parent tree, err := t.Where(t.ID.Eq(id)). Preload(t.GrandSons.RelationField). Preload(t.GrandDaughters.RelationField). First()

    Again this will only expand the first level of Associations

I see in the GORM a solution is to chain preload with nested structure here and here

But this works only for GORM, I cannot access the nested items in GORM-GEN

How can I achieve the topmost example response using GORM-GEN?

Comment From: sudopower

Creating it on GORM-GEN repo - link