为什么“属于”关系中的链接记录为空?

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

Why is a linked record in a "Belongs To" relation empty?

问题

以下是一个基于GORM的程序,定义了两个数据库表(userscards),其中用户拥有一张卡的逻辑。然后它创建数据库,填充数据,搜索并打印出其中唯一的记录。

我的问题是:在最后一次搜索中,卡片是空的,它没有与用户关联。

从数据库的角度来看,一切都没问题:

  1. SELECT * FROM users
  2. id name card_id
  3. 1 john 1
  4. SELECT * FROM cards
  5. id number
  6. 1 42

请注意,第一个结果中的card_id正确指向卡片的id

为什么我的最后一次搜索返回一个空的卡片?

  1. package main
  2. import (
  3. "fmt"
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. )
  7. type User struct {
  8. ID uint
  9. Name string
  10. UserCard Card
  11. CardID uint
  12. }
  13. type Card struct {
  14. ID uint
  15. Number string
  16. }
  17. func main() {
  18. // 初始化数据库
  19. db, _ := gorm.Open(sqlite.Open("mytest.sqlite"), &gorm.Config{})
  20. db.AutoMigrate(&User{}, &Card{})
  21. // 创建一张卡片
  22. db.Create(&Card{
  23. Number: "42",
  24. })
  25. // 查找那张卡片
  26. var myCard Card
  27. db.Where(map[string]interface{}{"number": "42"}).First(&myCard)
  28. // 创建一个带有那张卡片的用户
  29. db.Create(&User{
  30. Name: "john",
  31. UserCard: myCard,
  32. })
  33. // 查找那个用户
  34. var myUser User
  35. db.Where(map[string]interface{}{"name": "john"}).First(&myUser)
  36. // 打印他的名字和卡片号码
  37. // 问题出在这里:卡片号码为空,就好像卡片没有关联起来一样
  38. fmt.Printf("name: %v, number: %v", myUser.Name, myUser.UserCard.Number)
  39. }
  40. // 输出
  41. // name: john, number:
英文:

Below is a program (based on GORM) that defines two database tables (users and cards) with the logic of a user having one card. It then creates the database, fills it in, searches and prints out the only record in it.

My problem: the card in the ultimate search is empty, it is not attached to the user.

From the database perspective everything is OK:

  1. SELECT * FROM users
  2. id name card_id
  3. 1 john 1
  4. SELECT * FROM cards
  5. id number
  6. 1 42

Please note that card_id in the first result correctly points to the id of the card.

Why is my last search returning an empty card?

  1. package main
  2. import (
  3. "fmt"
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. )
  7. type User struct {
  8. ID uint
  9. Name string
  10. UserCard Card
  11. CardID uint
  12. }
  13. type Card struct {
  14. ID uint
  15. Number string
  16. }
  17. func main() {
  18. // initialize the database
  19. db, _ := gorm.Open(sqlite.Open("mytest.sqlite"), &gorm.Config{})
  20. db.AutoMigrate(&User{}, &Card{})
  21. // create one card
  22. db.Create(&Card{
  23. Number: "42",
  24. })
  25. // find that card
  26. var myCard Card
  27. db.Where(map[string]interface{}{"number": "42"}).First(&myCard)
  28. // create a user with that card
  29. db.Create(&User{
  30. Name: "john",
  31. UserCard: myCard,
  32. })
  33. // find that user
  34. var myUser User
  35. db.Where(map[string]interface{}{"name": "john"}).First(&myUser)
  36. // print his name and card number
  37. // the problem is here: the card number is empty, as if the card was not linked
  38. fmt.Printf("name: %v, number: %v", myUser.Name, myUser.UserCard.Number)
  39. }
  40. // output
  41. // name: john, number:

答案1

得分: 1

go-gorm不会自动加载嵌套对象,你需要明确指定你想要加载嵌套对象。在你的情况下,UserCardUser的嵌套对象。你可以在这里看到如何使用Preload函数来实现,但大致上应该是这样的:

  1. var myUser User
  2. db.Preload("UserCard").Where(map[string]interface{}{"name": "john"}).First(&myUser)
英文:

go-gorm will not load nested objects out of the box and automatically, you will have to specify that you want nested objects loaded. In your case, UserCard is a nested object of User. Here you can see more details on how to do that with the Preload function, but it should look something like this:

  1. var myUser User
  2. db.Preload("UserCard").Where(map[string]interface{}{"name": "john"}).First(&myUser)

huangapple
  • 本文由 发表于 2022年2月19日 04:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/71179594.html
匿名

发表评论

匿名网友

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

确定