doucang6914 2017-12-04 10:12
浏览 259

Gorm多次预加载不起作用

Good Monday Morning everyone! I am having a problem with preloading entities! I am doing multiple preloadings of entities. The Countryentity is working fine, but the Report is not being preloaded. Any ideas what might be causing that?

type Review struct {
  ID             int
  Report   Report 'gorm:"ForeignKey:ReportId"'
  ReportId uint
  Country        Country      'gorm:"ForeignKey:CountryId"'
  CountryId      uint
}
func FindApprovedReviews(page int, countryId int) ([]Review, int, error) {
  var reviews [] Review

// Subtract one to fix Offset but keep page number correct
  page -= 1
  err := db.Where("approved_at IS NOT NULL").
//TODO: Change this to a configuration file value
      Limit(10).
      Where("approved_by IS NOT NULL").
      Where("country_id = ?", countryId).
//TODO: Change this (the 10) to a configuration value
      Offset(page * 10).
      Order("id desc").
      Preload("Report").
      Preload("Country").
      Find(&reviews).
      Error
  if err != nil {
      return nil, 0, err
  }

  return reviews, count, err
}

The two related structs (Country and Report) are simply made up of gorm.Model for teting purpose.

Thanks in advance for the help!!

EDIT: I am using the mysql driver.

P.S. The [...] represent business data that has been hidden, but they are simple columns that are working fine.

  • 写回答

1条回答 默认 最新

  • douchai7891 2017-12-04 10:18
    关注

    Just realised I was selecting queries where the relation (i.e. FK) with the Report table was null.

    Works fine if the relation key is not null.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数