GORM 选择具有预加载的特定列

Wib*_*wan 1 go go-gorm

假设我有这些代表我的模型的结构

type QuestionHeader struct {
  QuestionHeaderId int    `gorm:"primaryKey;column:question_id" json:"question_id"`
  LevelId          int    `gorm:"column:level_id" json:"level_id"`
  SubjectId        int    `gorm:"column:subject_id" json:"subject_id"`
  QuestionBody     []QuestionBody
  QuestionSolution []QuestionSolution
  OtherColumn1A    string
  OtherColumn2A    string
}


type QuestionBody struct {
  QuestionBody  int    `gorm:"primaryKey;column:question_id" json:"question_id"`
  Value         string `gorm:"column:value" json:"value"`
  Type          string `gorm:"column:type" json:"type"`
  OtherColumn1B string
  OtherColumn2B string
}

type QuestionSolution struct {
  QuestionSolutionId int    `gorm:"primaryKey;column:question_id" json:"question_id"`
  Value              string `gorm:"column:value" json:"value"`
  Type               string `gorm:"column:type" json:"type"`
  OtherColumn1C      string
  OtherColumn2c      string
}
Run Code Online (Sandbox Code Playgroud)

然后我想像这样进行预加载查询

qs.db().
  Preload("QuestionHeader.QuestionBody", func(db *gorm.DB) *gorm.DB {
      return db.Where("question_body.status = ?", 1) 
       // I only want to select certain column here
  }).
  Preload("QuestionHeader.QuestionSolution", func(db *gorm.DB) *gorm.DB {
      return db.Where("question_solution.status = ?", 1) 
       // I only want to select certain column here
  }).
  Where("level_id = ?", 2).
  Find(&questionHeaders)  // I only want to select certain column here
Run Code Online (Sandbox Code Playgroud)

但该代码的问题是,我将根据我的模型属性选择所有列

如果我只想选择具有预加载的特定列怎么办?例如,我只想type选择QuestionBody

我知道您可以按照文档中的描述为 select 语句创建另一个结构但是还有其他方法吗?我不想为每个 select 语句创建一个结构

在 Laravel Eloquent 中,你可以做这样的事情

Cha*_*dan 5

有几个问题我想指出

  • 您没有在for或many2many中定义关联QuestionHeaderQuestionBodyQuestionSolution
  • 在查询中,您正在使用status列,但我似乎没有在QuestionBody或中定义它们QuestionSolution
  • QuestionBody将id 列重命名为QuestionBodyIdinQuestionBody

根据要点变化如下:

type QuestionHeader struct {
  QuestionHeaderId int                `gorm:"primaryKey;column:question_id" json:"question_id"`
  LevelId          int                `gorm:"column:level_id" json:"level_id"`
  SubjectId        int                `gorm:"column:subject_id" json:"subject_id"`
  QuestionBody     []QuestionBody     `gorm:"many2many:question_header_question_bodies;"`
  QuestionSolution []QuestionSolution `gorm:"many2many:question_header_question_solutions;"`
  OtherColumn1A    string
  OtherColumn2A    string
}

type QuestionBody struct {
  QuestionBodyId  int   `gorm:"primaryKey;column:question_id" json:"question_id"`
  Value         string  `gorm:"column:value" json:"value"`
  Type          string  `gorm:"column:type" json:"type"`
  Status        uint8   `gorm:"column:status;default:0" json:"status"`
  OtherColumn1B string
  OtherColumn2B string
}

type QuestionSolution struct {
  QuestionSolutionId int    `gorm:"primaryKey;column:question_id" json:"question_id"`
  Value              string `gorm:"column:value" json:"value"`
  Type               string `gorm:"column:type" json:"type"`
  Status             uint8  `gorm:"column:status;default:0" json:"status"`
  OtherColumn1C      string
  OtherColumn2c      string
}
Run Code Online (Sandbox Code Playgroud)
var questionHeaders QuestionHeader
db.
  Preload("QuestionBody", func(db *gorm.DB) *gorm.DB {
    return db.Where("Status = ?", 1).Select("QuestionBodyId", "Value")
  }).
  Preload("QuestionSolution", func(db *gorm.DB) *gorm.DB {
    return db.Where("Status = ?", 1).Select("QuestionSolutionId", "Value")
  }).
  Where("level_id = ?", 2).
  Select("SubjectId").
  Find(&questionHeaders)
}
Run Code Online (Sandbox Code Playgroud)

在预加载中,Select我们必须包含唯一的主键,以便gorm唯一地标识关联的结构切片