英文:
Why is a linked record in a "Belongs To" relation empty?
问题
以下是一个基于GORM的程序,定义了两个数据库表(users
和cards
),其中用户拥有一张卡的逻辑。然后它创建数据库,填充数据,搜索并打印出其中唯一的记录。
我的问题是:在最后一次搜索中,卡片是空的,它没有与用户关联。
从数据库的角度来看,一切都没问题:
SELECT * FROM users
id name card_id
1 john 1
SELECT * FROM cards
id number
1 42
请注意,第一个结果中的card_id
正确指向卡片的id
。
为什么我的最后一次搜索返回一个空的卡片?
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type User struct {
ID uint
Name string
UserCard Card
CardID uint
}
type Card struct {
ID uint
Number string
}
func main() {
// 初始化数据库
db, _ := gorm.Open(sqlite.Open("mytest.sqlite"), &gorm.Config{})
db.AutoMigrate(&User{}, &Card{})
// 创建一张卡片
db.Create(&Card{
Number: "42",
})
// 查找那张卡片
var myCard Card
db.Where(map[string]interface{}{"number": "42"}).First(&myCard)
// 创建一个带有那张卡片的用户
db.Create(&User{
Name: "john",
UserCard: myCard,
})
// 查找那个用户
var myUser User
db.Where(map[string]interface{}{"name": "john"}).First(&myUser)
// 打印他的名字和卡片号码
// 问题出在这里:卡片号码为空,就好像卡片没有关联起来一样
fmt.Printf("name: %v, number: %v", myUser.Name, myUser.UserCard.Number)
}
// 输出
// 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:
SELECT * FROM users
id name card_id
1 john 1
SELECT * FROM cards
id number
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?
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type User struct {
ID uint
Name string
UserCard Card
CardID uint
}
type Card struct {
ID uint
Number string
}
func main() {
// initialize the database
db, _ := gorm.Open(sqlite.Open("mytest.sqlite"), &gorm.Config{})
db.AutoMigrate(&User{}, &Card{})
// create one card
db.Create(&Card{
Number: "42",
})
// find that card
var myCard Card
db.Where(map[string]interface{}{"number": "42"}).First(&myCard)
// create a user with that card
db.Create(&User{
Name: "john",
UserCard: myCard,
})
// find that user
var myUser User
db.Where(map[string]interface{}{"name": "john"}).First(&myUser)
// print his name and card number
// the problem is here: the card number is empty, as if the card was not linked
fmt.Printf("name: %v, number: %v", myUser.Name, myUser.UserCard.Number)
}
// output
// name: john, number:
答案1
得分: 1
go-gorm
不会自动加载嵌套对象,你需要明确指定你想要加载嵌套对象。在你的情况下,UserCard
是User
的嵌套对象。你可以在这里看到如何使用Preload
函数来实现,但大致上应该是这样的:
var myUser User
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:
var myUser User
db.Preload("UserCard").Where(map[string]interface{}{"name": "john"}).First(&myUser)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论