我有模型:
type Book struct {
gorm.Model
Title string `json:"title"`
Author string `json:"author"`
Description string `json:"description"`
Category string `json:"Category"`
Publisher string `json:"publisher"`
AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard; "json:"authorscard"`
}
type AuthorsCard struct {
gorm.Model
Name string `json:"name"`
Age int `json:"age"`
YearOfBirth int `json:"year"`
Biography string `json:"biography"`
}
在 db.AutoMigrate(&models.Book{}, &models.AuthorsCard{})
我有一个创建函数:
func TestCreate() {
var testbook = models.Book{
Title: "Test",
Author: "tst",
AuthorsCard: []*models.AuthorsCard{
{
Age: 23,
Name: "test",
YearOfBirth: 1999,
Biography: "23fdgsdddTEST",
},
},
Description: "something",
}
db.Preload("AuthorsCard").Find(&models.Book{})
db.Create(&testbook)
}
尽管 AuthorsCard 中有一些数据,但我收到了以下回复:
{
"ID": 78,
"CreatedAt": "2022-06-23T13:10:58.01629+03:00",
"UpdatedAt": "2022-06-23T13:10:58.01629+03:00",
"DeletedAt": null,
"title": "Test",
"author": "tst",
"description": "something",
"Category": "",
"publisher": "",
"authorscard": null
}
如您所见,“authorscard”为空。
如何将“authorscard”保存到数据库?我使用了 Gorm + postgresql 并且我用邮递员发送了一些请求,结果是一样的 - 作者卡是空的
紫衣仙女
相关分类