go-gorm/gorm

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

hinego opened this issue · 4 comments

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)
	}
}

Hi,
I am working on it.

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