获取嵌套结构

我有以下结构:


type Users struct {

    ID           int64            `gorm:"id" json:"id"`

    Name         int64            `gorm:"name" json:"name"`

    UserTypeID   int              `gorm:"column:user_type_id" json:"-"`

    UserType     BusinnesUserType `gorm:"foreignKey:id;references:user_type_id" json:"user_type"`

    LanguageID   int64            `gorm:"column:language_id" json:"-"`

    Language     Language         `gorm:"foreignKey:ID;references:LanguageID" json:"language"`

}

type BusinnesUserType struct {

    ID          int64           `gorm:"id" json:"id"`

    Description json.RawMessage `gorm:"description" json:"description"`

}

type Language struct {

    ID          int64           `gorm:"id" json:"id"`

    Description json.RawMessage `gorm:"description" json:"description"`

}

我试图得到这样的东西:


{

   "id":1,

   "name":"dd",

   "user_type":{

      "id":1,

      "description":{

         "en":"admin"

      }

   },

   "language":{

      "id":1,

      "description":{

         "en":"English"

      }

   }

}

这是我对mysql的gorm查询:


db.Preload("UserType").Preload("Language").Where("id=?",1).Find(&user)

如果我使用Debug()选项,我会看到:


SELECT * FROM `languages` WHERE `languages`.`id` = 1 

SELECT * FROM `business_user_types` WHERE `business_user_types`.`id` = 1 

SELECT * FROM `users` WHERE `id` = 1 

但是我得到了以下json:


{

   "id":1,

   "name":"dd",

   "user_type":{

      "id":null,

      "description":null

   },

   "language":{

      "id":null,

      "description":null

   }

}

我以错误的方式使用预加载命令?我已经尝试过使用联接和关联,结果相同。


慕勒3428872
浏览 81回答 2
2回答

忽然笑

我认为用户模型应该看起来像这样..type Users struct {&nbsp; &nbsp; ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `gorm:"id" json:"id"`&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `gorm:"name" json:"name"`&nbsp; &nbsp; UserTypeID&nbsp; &nbsp;int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `gorm:"column:user_type_id" json:"-"`&nbsp; &nbsp; UserType&nbsp; &nbsp; &nbsp;BusinnesUserType `gorm:"foreignKey:UserTypeID;" json:"user_type"` // changed this line&nbsp; &nbsp; LanguageID&nbsp; &nbsp;int64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `gorm:"column:language_id" json:"-"`&nbsp; &nbsp; Language&nbsp; &nbsp; &nbsp;Language&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`gorm:"foreignKey:LanguageID;" json:"language"` // changed this line}更改了此行..Language&nbsp; &nbsp; &nbsp;Language&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`gorm:"foreignKey:LanguageID;" json:"language"`UserType&nbsp; &nbsp; &nbsp;BusinnesUserType `gorm:"foreignKey:UserTypeID;" json:"user_type"`编辑你必须手动外键// User : RelationsDB.Model(&models.Users{}).AddForeignKey("user_type_id", "Businnesusertypes(id)", "<CASCADE/RESTRICT>", "<CASCADE/RESTRICT>")DB.Model(&models.Users{}).AddForeignKey("language_id", "languages(id)", "<CASCADE/RESTRICT>", "<CASCADE/RESTRICT>")

青春有我

我已经找到了解决方案。问题出在“预加载”函数中,因为需要与预加载表的关系。现在,我的 gorm 查询是这样的:result := db.Preload("UserType", "id=?", &user.UserTypeID).&nbsp; &nbsp; &nbsp; &nbsp; Preload("Language", "id=?", &user.LanguageID).&nbsp; &nbsp; &nbsp; &nbsp; Where("business_users.id=?", 1).Find(&user)我的结构是这样的:type BusinessUsers struct {&nbsp; &nbsp; gorm.Model&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `gorm:"name" json:"name"`&nbsp; &nbsp; UserTypeID&nbsp; &nbsp; &nbsp;int64&nbsp; &nbsp; UserType&nbsp; &nbsp; &nbsp; &nbsp;BusinessUserType `gorm:"foreignkey:UserTypeID"`&nbsp; &nbsp; LanguageID&nbsp; &nbsp; &nbsp;int64&nbsp; &nbsp; Language&nbsp; &nbsp; &nbsp; &nbsp;Language `gorm:"foreignkey:LanguageID"`}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go