golang GORM和Gin无法创建具有关联的对象
问题内容
我使用 GORM 作为 ORM 以及我的 Golang API 来与我的数据库进行通信。
但是对于创建与数据库关联的实体,代码失败了(DB.Create(&data)
):
2023/11/01 20:44:14 [Recovery] 2023/11/01 - 20:44:14 panic recovered:
POST /v1/product/products HTTP/1.1
Host: localhost:8080
Accept: */*
Content-Length: 589
Content-Type: application/json
User-Agent: curl/8.4.0
runtime error: invalid memory address or nil pointer dereference
/usr/lib/go/src/runtime/panic.go:261 (0x452d97)
panicmem: panic(memoryError)
/usr/lib/go/src/runtime/signal_unix.go:861 (0x452d65)
sigpanic: panicmem()
/home/grimm/go/pkg/mod/gorm.io/[email protected]/finisher_api.go:18 (0x926e7c)
(*DB).Create: if db.CreateBatchSize > 0 {
/home/grimm/Documents/beeskill/website/back_end/API_website/main.go:122 (0x99cfab)
CreatingProduct: DB.Create(&data)
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x8c9ad9)
(*Context).Next: c.handlers[c.index](c)
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 (0x8c9ac7)
CustomRecoveryWithWriter.func1: c.Next()
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x8c8c7d)
(*Context).Next: c.handlers[c.index](c)
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:240 (0x8c8c4c)
LoggerWithConfig.func1: c.Next()
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x8c7d3a)
(*Context).Next: c.handlers[c.index](c)
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:620 (0x8c79cd)
(*Engine).handleHTTPRequest: c.Next()
/home/grimm/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:576 (0x8c74fc)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/lib/go/src/net/http/server.go:2938 (0x6a35ed)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go/src/net/http/server.go:2009 (0x69f4d3)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go/src/runtime/asm_amd64.s:1650 (0x46ed20)
goexit: BYTE $0x90 // NOP
登录后复制