我有以下相关表格:type Person struct { ID uint64 `json:"id" gorm:"primary_key;auto_increment"` Name string `json:"name"` Surname string `json:"surname"`}type Book struct { ID uint64 `json:"id" gorm:"primary_key;auto_increment"` Title string `json:"title" binding:"required,min=2,max=100" gorm:"type:varchar(100)"` Author Person `json:"author" binding:"required" gorm:"foreignkey:AuthorID"` // * here AuthorID uint64 `json:"-"` // * here WrittenIn string `json:"written_in" gorm:"type:varchar(5)"` CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP"` UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP"`}我可以通过使用此功能的Create()方法成功创建数据:gormfunc CreateBook(ctx *gin.Context) { // validating input var inputData CreateBookInput if err := ctx.ShouldBindJSON(&inputData); err != nil { ctx.JSON(401, gin.H{"status": "fail", "error": err.Error()}) } // create book book := models.Book{Title: inputData.Title, AuthorID: inputData.AuthorID, WrittenIn: inputData.WrittenIn} database.DB.Create(&book).Preload("Author") // database.DB.Preload("Author").Create(&book) // database.DB.Set("gorm:auto_preload", true).Create(&book) ctx.JSON(201, gin.H{"status": "success", "book": book})}我想把新创作的书连同它的作者一起归还。预期响应:"book": { "id": 10, "title": "Chinor ostidagi duel", "author": { "id": 3, "name": "John", "surname": "Smith" }, "written_in": "1983", "created_at": "2022-01-07T17:07:50.84473824+05:00", "updated_at": "2022-01-07T17:07:50.84473824+05:00"}但我找不到填充相关“作者”的方法。所以我得到的是:"book": { "id": 10, "title": "Chinor ostidagi duel", "author": { "id": 0, // empty "name": "", // empty "surname": "" // empty },如何填充新创建数据的相关字段?
2 回答

开满天机
TA贡献1786条经验 获得超13个赞
Preload是链式方法,Create是finisher方法。只有finisher 方法会生成并执行SQL。
所以...
1 创建书后按id查找作者
if err ;= database.DB.Create(&book).Error; err != nil {
return err
}
// will not throw not found error
database.DB.Limit(1).Find(&book.Author, book.AuthorID)
ctx.JSON(201, gin.H{"status": "success", "book": book})
2 每次加载作者数据,使用钩子
// create and update
// func (b *Book) AfterSave(tx *gorm.DB) (err error) {
// just create
func (b *Book) AfterCreate(tx *gorm.DB) (err error) {
// handle error if you want
tx.Limit(1).Find(&b.Author, b.AuthorID)
return
}
- 2 回答
- 0 关注
- 229 浏览
添加回答
举报
0/150
提交
取消