Postgresql Gorm返回不同的时间戳格式,然后用于记录创建

Postgresql Gorm返回不同的时间戳格式,然后用于记录创建,postgresql,go,timestamp,go-gorm,go-gin,Postgresql,Go,Timestamp,Go Gorm,Go Gin,我正在使用GORM从使用gin的Go应用程序中创建记录。我在gorm.Config文件中指定了一个gorm文档中指定的NowFunc 下面是一个使用gin和gorm的完整封装示例应用程序,它演示了我试图解决的问题: package main import ( "github.com/gin-gonic/gin" "gorm.io/driver/postgres" "gorm.io/gorm" "

我正在使用GORM从使用gin的Go应用程序中创建记录。我在
gorm.Config
文件中指定了一个gorm文档中指定的NowFunc

下面是一个使用gin和gorm的完整封装示例应用程序,它演示了我试图解决的问题:

package main

import (
    "github.com/gin-gonic/gin"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "net/http"
    "strconv"
    "time"
)
var db *gorm.DB

type Product struct {
    gorm.Model
    Code  string
    Price uint
}

func handlePost(c *gin.Context, db *gorm.DB) {
    var product Product
    if err := c.ShouldBind(&product); err != nil {
        c.JSON(http.StatusBadRequest, err)
        return
    }
    db.Debug().Create(&product).Debug()
    c.JSON(http.StatusCreated, product)
}

func handleGet(c *gin.Context, db *gorm.DB) {
    id, err := strconv.ParseInt(c.Param("id"), 10, 64)
    if err != nil {
        _ = c.Error(err)
    }
    var product Product
    product.ID = uint(id)
    db.Debug().Find(&product)
    c.JSON(http.StatusOK, product)
}

func timeformat() time.Time {
    return time.Now().UTC().Truncate(time.Microsecond)
}

func main() {
    dsn := "host=localhost port=5432 dbname=gorm sslmode=disable connect_timeout=5 application_name='gorm test'"
    config := &gorm.Config{NowFunc: timeformat}
    // PROBLEM DOESN'T OCCUR WHEN USING SQL-LITE DB
    //  database, err := gorm.Open(sqlite.Open("test.db"), config)
    // PROBLEM OCCURs WHEN USING POSTGRES DB
    database, err := gorm.Open(postgres.Open(dsn), config)
    if err != nil {
        panic("failed to connect database")
    }
    // Migrate the schema
    database.AutoMigrate(&Product{})
    db = database

    router := gin.Default()

    router.GET("/get/:id", func(c *gin.Context) { handleGet(c, db) })
    router.POST("/post", func(c *gin.Context) { handlePost(c, db) })
    router.Run(":8080")
}
当我运行此应用程序并发送以下创建记录的请求时,如下所示:

curl --location --request POST 'localhost:8080/post/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "Code": "AAA"
}'
curl --location --request GET 'localhost:8080/get/1'
我收到以下答复:

{
    "ID": 1,
    "CreatedAt": "2021-04-16T15:48:59.749294Z",
    "UpdatedAt": "2021-04-16T15:48:59.749294Z",
    "DeletedAt": null,
    "Code": "AAA",
    "Price": 0
}
注意,时间戳的格式为指定的NowFunc。但是,如果我按如下方式检索此记录:

curl --location --request POST 'localhost:8080/post/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "Code": "AAA"
}'
curl --location --request GET 'localhost:8080/get/1'
我收到以下记录:

{
    "ID": 1,
    "CreatedAt": "2021-04-16T11:48:59.749294-04:00",
    "UpdatedAt": "2021-04-16T11:48:59.749294-04:00",
    "DeletedAt": null,
    "Code": "AAA",
    "Price": 0
}
所以问题是,为什么我不接收GET请求中的记录,其时间戳格式与POST响应中的相同

更新
使用anSQL LITE数据库时不会出现这种情况。

经过大量研究,问题是Go的默认时间精度是纳秒,而Postgres SQL的精度是微秒。以下库解决了我的问题:


经过大量研究,问题在于Go的默认时间精度是纳秒,而Postgres SQL的精度是微秒。以下库解决了我的问题: