Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates cant auto set updated_at when strcut dont have Updated_at field. #7226

Open
hinego opened this issue Oct 6, 2024 · 4 comments
Open
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@hinego
Copy link

hinego commented Oct 6, 2024

GORM Playground Link

[<!--
To ensure your issue be handled, the issue MUST include a GORM Playground Pull Request Link that can reproduce the bug, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed, refer: https://github.com/go-gorm/playground

Without the link, your issue most likely will be IGNORED

CHANGE FOLLOWING URL TO YOUR PLAYGROUND LINK
-->

go-gorm/playground#761

Description

package main

import (
	"log"

	"github.com/gogf/gf/v2/util/grand"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Route struct {
	ID        uint `gorm:"primarykey"`
	CreatedAt int64
	UpdatedAt int64
	Name      string
}
type Update struct {
	Name      string
	UpdatedAt int64
}
type NoUpdate struct {
	Name string
}

func main() {
	// 连接到 MySQL 数据库
	dsn := "root:xxxx@tcp(xxxx:3306)/xxxx?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
	}

	var route = &Route{}
	// 使用 Updates 方法更新 Route
	//[7.219ms] [rows:1] UPDATE `routes` SET `updated_at`=1728225917,`name`='KgqHaJnfID' WHERE `id` = 5
	result := db.Model(&route).Debug().Where("id", 5).Updates(Route{Name: grand.Letters(10)})
	if result.Error != nil {
		log.Fatalf("failed to update route: %v", result.Error)
	}
	//[8.108ms] [rows:1] UPDATE `routes` SET `updated_at`=1728225917,`name`='zPrQbQopwm' WHERE `id` = 5
	result = db.Model(&route).Debug().Where("id", 5).Updates(Update{Name: grand.Letters(10)})
	if result.Error != nil {
		log.Fatalf("failed to update route: %v", result.Error)
	}
        // 这里 如果 NoUpdate 没有 UpdatedAt 时将不会自动更新 updated_at 但是 转成map后是正常可以的 (即使没有uodated_at字段)
	//[7.656ms] [rows:1] UPDATE `routes` SET `name`='NkGiUbrINK' WHERE `id` = 5
	result = db.Model(&route).Debug().Where("id", 5).Updates(NoUpdate{Name: grand.Letters(10)})
	if result.Error != nil {
		log.Fatalf("failed to update route: %v", result.Error)
	}
}

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Oct 6, 2024
@Mghasemzadeh
Copy link

Hi,
I am working on it.

@Mghasemzadeh
Copy link

Hi again,
I’m currently working on this issue. I’ve successfully reproduced the problem and identified the specific section of the code that’s causing it. I’m now focusing on finding a solution

@Mghasemzadeh
Copy link

Mghasemzadeh commented Nov 10, 2024

Hi,

You should change the struct field type to time.Time and use the gorm:"autoCreateTime" annotation for automatic handling of the created_at field, and gorm:"autoUpdateTime" for the updated_at field. This ensures that GORM automatically manages the timestamps during record creation and updates.
Additionally, make sure that the corresponding database columns are of type TIMESTAMP. Changing the field type to time.Time in your Go struct will ensure that GORM properly maps these fields to MySQL's TIMESTAMP data type, allowing for correct storage and automatic timestamp handling without manual intervention.

@hinego

@Mghasemzadeh
Copy link

Hi @jinzhu,

I've investigated this issue and found that it’s not valid as described. The current functionality meets the expected behavior, and no changes are necessary at this time. So, I recommend closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

3 participants