Updates in Gorm use created_at, updated_at as default times

王林
Release: 2024-02-05 23:21:03
forward
1226 people have browsed it

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

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
}
Copy after login

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"
    }
}
Copy after login


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
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!