Golang Gorm无法从关联表中检索数据。

huangapple go评论118阅读模式
英文:

Golang Gorm not retrieving data from associated table

问题

我正在使用Gorm和MySQL开发一个Gin应用程序。为了在Gorm模型中定义“属于”关系,你需要按照以下步骤进行操作(示例取自Gorm文档):

  1. // `User`属于`Company`,`CompanyID`是外键
  2. type User struct {
  3. gorm.Model
  4. Name string
  5. CompanyID int
  6. Company Company
  7. }
  8. type Company struct {
  9. ID int
  10. Name string
  11. }

这是我在我的模型中所做的:

  1. type Record struct {
  2. Barcode string `json:"barcode" gorm:"size:48;unique;not null" sql:"index"`
  3. Name string `json:"name" gorm:"size:160;unique;not null"`
  4. ArtistID uint `json:"artist_id"`
  5. Artist Artist `gorm:"foreignKey:ArtistID;references:ID"`
  6. CategoryID uint `json:"category_id"`
  7. Category Category `gorm:"foreignKey:CategoryID;references:ID"`
  8. NumOfRecords int `json:"num_of_records" gorm:"not null"`
  9. OriginalReleaseDate *utils.Date `json:"original_release_date" gorm:"default:null;type:date"`
  10. ReissueReleaseDate *utils.Date `json:"reissue_release_date" gorm:"default:null;type:date"`
  11. SideColor *string `json:"side_color" gorm:"default:null"`
  12. BarcodeInRecord *bool `json:"barcode_in_record" gorm:"default:true"`
  13. gorm.Model
  14. }
  1. type Category struct {
  2. Name string `json:"name" gorm:"size:60;unique;not null"`
  3. Description string `json:"description" gorm:"size:120"`
  4. Parent uint `json:"parent" gorm:"default:null"`
  5. Active bool `json:"active" gorm:"default:true"`
  6. gorm.Model
  7. }
  1. type Artist struct {
  2. Name string `json:"name" gorm:"size:120;unique;not null"`
  3. Type string `json:"type" gorm:"default:null"`
  4. CountryOfOrigin string `json:"countryOfOrigin" gorm:"default:null"`
  5. gorm.Model
  6. }

然而,在获取数据时,这两个关联对象没有被填充:

  1. {
  2. "data": {
  3. "barcode": "1231231231231292",
  4. "name": "ABCD 12342",
  5. "artist_id": 2,
  6. "Artist": {
  7. "name": "",
  8. "type": "",
  9. "countryOfOrigin": "",
  10. "ID": 0,
  11. "CreatedAt": "0001-01-01T00:00:00Z",
  12. "UpdatedAt": "0001-01-01T00:00:00Z",
  13. "DeletedAt": null
  14. },
  15. "category_id": 9,
  16. "Category": {
  17. "name": "",
  18. "description": "",
  19. "parent": 0,
  20. "active": false,
  21. "ID": 0,
  22. "CreatedAt": "0001-01-01T00:00:00Z",
  23. "UpdatedAt": "0001-01-01T00:00:00Z",
  24. "DeletedAt": null
  25. },
  26. "num_of_records": 2,
  27. "original_release_date": "1965-02-24",
  28. "reissue_release_date": null,
  29. "side_color": null,
  30. "barcode_in_record": null,
  31. "ID": 1,
  32. "CreatedAt": "2022-04-25T12:53:32.275578-04:00",
  33. "UpdatedAt": "2022-04-25T12:53:32.275578-04:00",
  34. "DeletedAt": null
  35. }
  36. }

有什么想法是怎么回事吗?

英文:

I'm working on a Gin app using Gorm with MySQL. In order to define a belongs to relationship in a Gorm Model, you have to do the following (example taken from Gorm docs):

  1. // `User` belongs to `Company`, `CompanyID` is the foreign key
  2. type User struct {
  3. gorm.Model
  4. Name string
  5. CompanyID int
  6. Company Company
  7. }
  8. type Company struct {
  9. ID int
  10. Name string
  11. }

That's what I did with my models:

  1. type Record struct {
  2. Barcode string `json:"barcode" gorm:"size:48;unique;not null" sql:"index"`
  3. Name string `json:"name" gorm:"size:160;unique;not null"`
  4. ArtistID uint `json:"artist_id"`
  5. Artist Artist `gorm:"foreignKey:ArtistID;references:ID"`
  6. CategoryID uint `json:"category_id"`
  7. Category Category `gorm:"foreignKey:CategoryID;references:ID"`
  8. NumOfRecords int `json:"num_of_records" gorm:"not null"`
  9. OriginalReleaseDate *utils.Date `json:"original_release_date" gorm:"default:null;type:date"`
  10. ReissueReleaseDate *utils.Date `json:"reissue_release_date" gorm:"default:null;type:date"`
  11. SideColor *string `json:"side_color" gorm:"default:null"`
  12. BarcodeInRecord *bool `json:"barcode_in_record" gorm:"default=true"`
  13. gorm.Model
  14. }
  1. type Category struct {
  2. Name string `json:"name" gorm:"size:60;unique;not null"`
  3. Description string `json:"description" gorm:"size:120"`
  4. Parent uint `json:"parent" gorm:"default:null"`
  5. Active bool `json:"active" gorm:"default:true"`
  6. gorm.Model
  7. }
  1. type Artist struct {
  2. Name string `json:"name" gorm:"size:120;unique;not null"`
  3. Type string `json:"type" gorm:"default:null"`
  4. CountryOfOrigin string `json:"countryOfOrigin" gorm:"default:null"`
  5. gorm.Model
  6. }

yet when getting data back, those two associations are not being populated:

  1. {
  2. "data": {
  3. "barcode": "1231231231231292",
  4. "name": "ABCD 12342",
  5. "artist_id": 2,
  6. "Artist": {
  7. "name": "",
  8. "type": "",
  9. "countryOfOrigin": "",
  10. "ID": 0,
  11. "CreatedAt": "0001-01-01T00:00:00Z",
  12. "UpdatedAt": "0001-01-01T00:00:00Z",
  13. "DeletedAt": null
  14. },
  15. "category_id": 9,
  16. "Category": {
  17. "name": "",
  18. "description": "",
  19. "parent": 0,
  20. "active": false,
  21. "ID": 0,
  22. "CreatedAt": "0001-01-01T00:00:00Z",
  23. "UpdatedAt": "0001-01-01T00:00:00Z",
  24. "DeletedAt": null
  25. },
  26. "num_of_records": 2,
  27. "original_release_date": "1965-02-24",
  28. "reissue_release_date": null,
  29. "side_color": null,
  30. "barcode_in_record": null,
  31. "ID": 1,
  32. "CreatedAt": "2022-04-25T12:53:32.275578-04:00",
  33. "UpdatedAt": "2022-04-25T12:53:32.275578-04:00",
  34. "DeletedAt": null
  35. }
  36. }

any idea what's going on there?

答案1

得分: 2

保存数据到数据库的代码是怎样的?

我猜你可能需要使用FullSaveAssociations,类似这样:

  1. DB().Session(&gorm.Session{FullSaveAssociations: true}).Save(&d)

要获取数据,你需要使用Preloads。

  1. DB().Preload("Artist").Preload("Category").First(&d)

你还可以使用

  1. .Preload(clause.Associations)

来加载它们。你可以与其他Preloads一起使用,以深入加载。

https://gorm.io/docs/preload.html#content-inner

英文:

What does the code look like that is saving the data to the db?

I'm guessing that you probably need to use FullSaveAssociations
something like this:

  1. DB().Session(&gorm.Session{FullSaveAssociations: true}).Save(&d)

To get the data back, you need to use Preloads.

  1. DB().Preload("Artist").Preload("Category").First(&d)

you can also use

  1. .Preload(clause.Associations)

to load them all. You can use this with other Preloads to go deeper.

https://gorm.io/docs/preload.html#content-inner

huangapple
  • 本文由 发表于 2022年4月26日 01:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/72003273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定