GORM GIN Update查询结果在500内部服务器错误



我正在使用Gin和GORM在Golang中构建一个Book API。我已经成功地实现了API的创建,获取和删除部分,但更新导致500内部服务器错误。我试图为一个更新功能,它只更新正文中提供的特定标签(下面给出的例子)。任何帮助或指导都是非常感谢的。

<<p>去日志/strong>
[31m2022/05/09 14:08:00 [Recovery] 2022/05/09 - 14:08:00 panic recovered:
PATCH /books/2 HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 66
Content-Type: text/plain;charset=UTF-8
Origin: chrome-extension://ihgpcfpkpmdcghlnaofdmjkoemnlijdi
Sec-Ch-Ua: " Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Windows"
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: none
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36

reflect: call of reflect.Value.SetString on uint Value
C:/Program Files/Go/src/reflect/value.go:221 (0xc7bb04)
flag.mustBe: panic(&ValueError{methodName(), f.kind()})
C:/Program Files/Go/src/reflect/value.go:1747 (0xc7ba3e)
Value.SetString: v.mustBe(String)
C:/Users/NK086843/go/pkg/mod/gorm.io/gorm@v1.23.5/schema/field.go:771 (0x10defb7)
(*Field).setupValuerAndSetter.func11: field.ReflectValueOf(ctx, value).SetString(data)
C:/Users/NK086843/go/pkg/mod/gorm.io/gorm@v1.23.5/callbacks/update.go:144 (0x115b68e)
ConvertToAssignments.func2: field.Set(stmt.Context, stmt.ReflectValue, value)
C:/Users/NK086843/go/pkg/mod/gorm.io/gorm@v1.23.5/callbacks/update.go:275 (0x11534f9)
ConvertToAssignments: assignValue(field, value)
C:/Users/NK086843/go/pkg/mod/gorm.io/gorm@v1.23.5/callbacks/update.go:73 (0x115ac9e)
Update.func1: if set := ConvertToAssignments(db.Statement); len(set) != 0 {
C:/Users/NK086843/go/pkg/mod/gorm.io/gorm@v1.23.5/callbacks.go:130 (0x10ebe14)
(*processor).Execute: f(db)
C:/Users/NK086843/go/pkg/mod/gorm.io/gorm@v1.23.5/finisher_api.go:372 (0x10f4e8d)
(*DB).Updates: return tx.callbacks.Update().Execute(tx)
C:/New folder/golang/github.com/shashank-kakarla/BookAPI/controllers/books.go:101 (0x1167007)
UpdateBook: models.DB.Model(&book).Updates(input)
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/context.go:168 (0x10917e1)
(*Context).Next: c.handlers[c.index](c)
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/recovery.go:99 (0x10917c7)
CustomRecoveryWithWriter.func1: c.Next()
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/context.go:168 (0x10908c6)
(*Context).Next: c.handlers[c.index](c)
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/logger.go:241 (0x1090885)
LoggerWithConfig.func1: c.Next()
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/context.go:168 (0x1086689)
(*Context).Next: c.handlers[c.index](c)
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/gin.go:555 (0x108666f)
(*Engine).handleHTTPRequest: c.Next()
C:/Users/NK086843/go/pkg/mod/github.com/gin-gonic/gin@v1.7.7/gin.go:511 (0x1086112)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Program Files/Go/src/net/http/server.go:2867 (0xe76e29)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
C:/Program Files/Go/src/net/http/server.go:1932 (0xe721ac)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
C:/Program Files/Go/src/runtime/asm_amd64.s:1371 (0xc3d900)
goexit: BYTE    $0x90   // NOP
←[0m
[GIN] 2022/05/09 - 14:08:00 |←[97;41m 500 ←[0m|     19.7185ms |       127.0.0.1 |←[97;42m PATCH   ←[0m "/books/2"
<<p>去模型/strong>
func UpdateBook(c *gin.Context) {
// Get model if exist
var book models.Book
if err := models.DB.Where("id = ?", c.Param("id")).First(&book).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
// Validate input
var input UpdateBookInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
models.DB.Model(&book).Updates(input)
c.JSON(http.StatusOK, gin.H{"data": book})
}

UpdateBookInput Struct

type UpdateBookInput struct {
Title  string `json:"title"`
Author string `json:"author"`
}

main.go

func main() {
router := gin.Default()
models.ConnectDatabase()
router.GET("/books", controllers.FetchBooks)
router.GET("/books/:id", controllers.FindBookByID)
router.GET("/books/title/:title", controllers.FindBookByTitle)
router.GET("/books/author/:author", controllers.FindBookByAuthor)
router.POST("/books", controllers.CreateBook)
router.PATCH("/books/:id", controllers.UpdateBook)
router.DELETE("/books/:id", controllers.RemoveBook)
router.Run()
}

请求URL和BodyURL

PATCH http://127.0.0.1:8080/books/2

身体

{
"title": "Breaking Dawn",
}
<<p>模式/模型/strong>
package models
type Book struct {
ID     uint   `json:"id" gorm:"primary_key"`
Title  string `json:"title"`
Author string `json:"author"`
}

我假设JSON数据包含一个字符串类型的ID字段,因为这里唯一的int字段是ID。要么是gorm对更新的行为很奇怪,要么是这样的。

确保输入是干净的。

不直接传递UpdateBookInput对象,而是将其转换为models.Book对象。

最新更新