Home >Backend Development >Golang >Updates in Gorm use created_at, updated_at as default times

Updates in Gorm use created_at, updated_at as default times

王林
王林forward
2024-02-05 23:21:031341browse

Gorm 中的更新将created_at、updated_at 作为默认时间

Question content

I am trying to update an entity post in the database using gorm, the update is reflected correctly in the database but in the object returned by the gorm package , the created_at and Updated_at fields are the default time, which is "0001/01/01..." p>

My database model is

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;"`
}

query is

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
}

For reference only, the API response is -

{
    "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"
    }
}

Correct answer


You should try to manually set the UpdatedAt field when updating a record. Something like this:

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
}

The above is the detailed content of Updates in Gorm use created_at, updated_at as default times. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete