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 -->
https://github.com/go-gorm/playground/pull/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)
}
}
Comment From: Mghasemzadeh
Hi, I am working on it.
Comment From: Mghasemzadeh
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
Comment From: Mghasemzadeh
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
Comment From: Mghasemzadeh
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.
Comment From: WUYONGGANG
哥 能不能探讨一下 更好了实现gorm-gen 结构体转查询语句的方案呀?
Comment From: WUYONGGANG
想联系你 不知道怎么联系 谢谢 @hinego