我正在使用GORM for the 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/gorm@v1.25.5/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/gin@v1.9.1/context.go:174 (0x8c9ad9)
        (*Context).Next: c.handlers[c.index](c)
/home/grimm/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x8c9ac7)
        CustomRecoveryWithWriter.func1: c.Next()
/home/grimm/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x8c8c7d)
        (*Context).Next: c.handlers[c.index](c)
/home/grimm/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0x8c8c4c)
        LoggerWithConfig.func1: c.Next()
/home/grimm/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x8c7d3a)
        (*Context).Next: c.handlers[c.index](c)
/home/grimm/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x8c79cd)
        (*Engine).handleHTTPRequest: c.Next()
/home/grimm/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/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

使用的代码:

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

type CategoryProduct struct {
    gorm.Model
    Title       string `json:"title" gorm:"not null"`
    Description string `json:"description"`
}

type CreateCategoryProduct struct {
    gorm.Model
    Title       string `json:"title" binding:"required"`
    Description string `json:"description" binding:"required"`
}

// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
type PriceYearProduct struct {
    gorm.Model
    Price       float64 `json:"price" gorm:"not null"`
    Description string  `json:"description"`
}

type CreatePriceYearProduct struct {
    gorm.Model
    Price       float64 `json:"price" binding:"required"`
    Description string  `json:"description" binding:"required"`
}

// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
type PriceMounthProduct struct {
    gorm.Model
    Price       float64 `json:"price" gorm:"not null"`
    Description string  `json:"description"`
}

type CreatePriceMounthProduct struct {
    gorm.Model
    Price       float64 `json:"price" binding:"required"`
    Description string  `json:"description" binding:"required"`
}

// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
// --------------------------------------------------------
type Product struct {
    gorm.Model
    Name               string             `json:"name" gorm:"not null"`
    Description        string             `json:"description" gorm:"not null"`
    PriceMounthProduct PriceMounthProduct `json:"pricemounth"`
    PriceYearProduct   PriceYearProduct   `json:"priceyear"`
    CategoryProduct    CategoryProduct    `json:"category"`
}

type CreateProduct struct {
    gorm.Model
    Name               string             `json:"name" binding:"required"`
    Description        string             `json:"description" binding:"required"`
    PriceMounthProduct PriceMounthProduct `json:"pricemounth" binding:"required"`
    PriceYearProduct   PriceYearProduct   `json:"priceyear" binding:"required"`
    CategoryProduct    CategoryProduct    `json:"category" binding:"required"`
}

var DB *gorm.DB

func ConnectDatabase() {

    database, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})

    if err != nil {
        panic("Failed to connect to database!")
    }
    // For the product and dependancies of them
    err = database.AutoMigrate(&CategoryProduct{}, &PriceYearProduct{}, &PriceMounthProduct{})
    if err != nil {
        return
    }
    err = database.AutoMigrate(&Product{})
    if err != nil {
        return
    }

    DB = database
}

// POST /v1/products
// Create a product
func CreatingProduct(c *gin.Context) {
    // Validate input
    var input CreateProduct
    if err := c.ShouldBindJSON(&input); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    data := Product{
        Name:               input.Name,
        Description:        input.Description,
        PriceYearProduct:   input.PriceYearProduct,
        PriceMounthProduct: input.PriceMounthProduct,
        CategoryProduct:    input.CategoryProduct,
    }
    DB.Create(&data)

    c.JSON(http.StatusOK, gin.H{"data": data})

}

func Routes() {
    var router = gin.Default()

    v1 := router.Group("/v1")
    {
        //Product CRUD
        product := v1.Group("/product")
        product.POST("/products", CreatingProduct)

    }

    err := router.Run(":8080")
    if err != nil {
        return
    }
}

func main() {
    ConnectDatabase()
    Routes()

}

以及用于发布一些数据的curl命令:

curl http://localhost:8080/v1/product/products --request "POST" --header "Content-Type: application/json" --data @file.json

文件.json的内容:

{
    "name": "airplane simulation",
    "description": "airplane simulation for everybody, childrens to adults.",
    "priceyear": {
      "price": 44.99,
      "description": "pay for a year to access on irplane simulation for everybody, childrens to adults."
    },
    "pricemounth": {
      "price": 4.99,
      "description": "pay for a mounth to access on irplane simulation for everybody, childrens to adults."
    },
    "category": {
      "title": "Airplane",
      "description": "Information cataegorized on airplane."
    }
}

我已经看了官方文件,但没有更多的信息. try 使用创建时的变量进行调试,但似乎一切正常…

推荐答案

运行您的代码时出错

[error] invalid field found for struct
 main.Product's field PriceMounthProduct: 
define a valid foreign key for 
relations or implement the Valuer/Scanner interface

GIN日志(log)之前第一行的错误消息可能是您错过了它.

所以修复它,在Product struct 中添加正确的foreignkey gorm tag就可以了.


type Product struct {
    gorm.Model
    Name               string             `json:"name" gorm:"not null"`
    Description        string             `json:"description" gorm:"not null"`
    PriceMounthProduct PriceMounthProduct `json:"pricemounth" gorm:"foreignkey:ID"`
    PriceYearProduct   PriceYearProduct   `json:"priceyear" gorm:"foreignkey:ID"`
    CategoryProduct    CategoryProduct    `json:"category" gorm:"foreignkey:ID"`
}

并运行

curl http://localhost:8080/v1/product/products --request "POST" --header "Content-Type: application/json" --data @info.json

修得

{"data":{"ID":1,"CreatedAt":"2023-11-02T13:37:24.052228+08:00","UpdatedAt":"2023-11-02T13:37:24.052228+08:00","DeletedAt":null,"name":"airplane simulation","description":"airplane simulation for everybody, childrens to adults.","pricemounth":{"ID":1,"CreatedAt":"2023-11-02T13:37:24.054792+08:00","UpdatedAt":"2023-11-02T13:37:24.054792+08:00","DeletedAt":null,"price":4.99,"description":"pay for a mounth to access on irplane simulation for everybody, childrens to adults."},"priceyear":{"ID":1,"CreatedAt":"2023-11-02T13:37:24.056352+08:00","UpdatedAt":"2023-11-02T13:37:24.056352+08:00","DeletedAt":null,"price":44.99,"description":"pay for a year to access on irplane simulation for everybody, childrens to adults."},"category":{"ID":1,"CreatedAt":"2023-11-02T13:37:24.056585+08:00","UpdatedAt":"2023-11-02T13:37:24.056585+08:00","DeletedAt":null,"title":"Airplane","description":"Information cataegorized on airplane."}}}% 

Database相关问答推荐

Spring Data JPA 对 SQL 注入安全吗

如何在 SQL Server 中将索引从一个表复制到另一个表

只用一个 save() 插入多行

PostgreSQL - 按时间戳值分组?

数据库供应商如何实现事务?

按请求的可变事务隔离级别

如何设计 SaaS 数据库?

当可伸缩性无关紧要时,NoSQL 与 SQL

是否应该强制执行参照完整性?

从 XML 读取数据

与号 (&) 处的 XML 错误

设置默认数据库连接 Rails

HTML5 数据库存储 (SQL lite) - 几个问题

有什么理由不应该在生产中使用 h2 数据库?

mysql 无法从存储引擎读取自增值

如何在实体框架中使用字符串属性作为主键

显式事务回滚是否必要?

Python中的内存数据库

单元测试数据库

最小覆盖和功能依赖