Selecting related fields with GORM

◇◆丶佛笑我妖孽 提交于 2019-12-01 17:47:06

According to documentation, you need to set auto_preload to true for Auto Preloading to work. So changing the line where you retrieve the user to:

common.DB.Set("gorm:auto_preload", true).First(&retrivedUser, 1)

should give you the expected result.


On a side note, never ignore the errors. You can always append .Error to almost every operation in gorm and check the returned error. For example:

err = common.DB.AutoMigrate(...).Error
if err != nil {
    // handle err
}

err = common.DB.Create(&user).Error
// Check for err
err = common.DB.Set("gorm:auto_preload", true).First(&retrivedUser, 1).Error
// Check for err
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!