我正在try 使用GORM更新数据库中的实体帖子,更新正确地反映在数据库中,但在GORM包返回的对象中,创建的_at和更新的_at字段是默认时间,即‘0001/01/01...’

我的数据库模特是

type Base struct {
    ID        uuid.UUID       `json:"id" gorm:"primaryKey;default:gen_random_uuid();not null"`
    CreatedAt time.Time       `json:"created_at" gorm:"default:now()"`
    UpdatedAt time.Time       `json:"updated_at" gorm:"default:now()"`
    DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at" swaggertype:"primitive,string"`
}



type Post struct {
    Base
    Title       string    `json:"title" gorm:"column:title"`
    TLDR        string    `json:"tldr" gorm:"column:tldr"`
    HTML        string    `json:"html" gorm:"column:html"`
    JSON        string    `json:"json" gorm:"column:json"`
    BannerImage string    `json:"banner_image" gorm:"column:banner_image"`
    Slug        string    `json:"slug" gorm:"column:slug"`
    Status      Status    `json:"status" gorm:"column:status"`
    AuthorID    uuid.UUID `json:"author_id" gorm:"column:author_id;"`
}

该查询为

func (p *pgPostRepository) UpdatePost(postDetails *models.Post) (*models.Post, error) {
    db := p.db.GetClient().(*gorm.DB)
    err := db.Model(&models.Post{}).Where(&models.Post{
        Base: models.Base{
            ID: postDetails.ID,
        },
    }).Updates(&postDetails).Error
    if err != nil {
        return nil, err
    }
    return postDetails, nil
}

仅供参考,API响应为-

{
    "message": "post updated successfully",
    "post": {
        "id": "e4645d79-51da-4e08-85d3-e7409ad6b77b",
        "created_at": "0001-01-01T00:00:00Z",
        "updated_at": "0001-01-01T00:00:00Z",
        "deleted_at": null,
        "title": "Sample Title",
        "tldr": "Brief summary of the content",
        "html": "<p>This is the HTML content.</p>",
        "json": "{\"key\": \"value\"}",
        "banner_image": "https://example.com/image.jpg",
        "slug": "sample-title-1694539437151872840",
        "status": "draft",
        "author_id": "96fa719c-358e-4c5d-b878-daee02e2c38b"
    }
}

推荐答案

在更新记录时,应try 手动设置UpdatedAt字段.大概是这样的:

func (p *pgPostRepository) UpdatePost(postDetails *models.Post) (*models.Post, error) {
    db := p.db.GetClient().(*gorm.DB)

    postDetails.UpdatedAt = time.Now()

    err := db.Model(&models.Post{}).Where(&models.Post{
        Base: models.Base{
            ID: postDetails.ID,
        },
    }).Updates(&postDetails).Error

    if err != nil {
        return nil, err
    }
    
    return postDetails, nil
}

Postgresql相关问答推荐

如何在Common Lisp中使用Postmodern在表更改时获得通知?

用于JSON数组的带有组合条件的Postgres JSONB Select 查询

在Axum 0.5中,如何在一个请求处理程序中同时使用数据库和路径解析?

使用postgres在go测试容器中使用tern迁移

PL/pgSQL中的IP递增函数

正在加载 pgAdmin 4 v7.4...同时打开 pgAdmin

PostgreSQL:如何从另一列的 json 值替换为一列?

如何在postgresql中按时间查询

无法从外部连接到在 AWS EC2 实例上运行的 Postgres

Postgres - 如何加入两列?

如何在 Sequelize ORM 中插入 PostGIS GEOMETRY 点?

postgres 的密码

Hibernate 将用户模型保存到 Postgres

Docker - 判断 postgres 是否准备好

我应该使用哪个 postgresql 包?

Select 空字段

将 PostgreSQL 配置为仅适用于 LOCALHOST 或指定的 ip + 端口

Postgres 删除表语法错误

从time without time zone和时区名称中获取time with time zone

在 PostgreSQL 中将列数据类型从 Text 更改为 Integer