Update with serializer:unixtime does not insert timestamps properly
Description
Here's my code, the database mysql:8.0.0 field is the timestamp type. If I don't specify serializer:unixtime, I can't plug in. There is no problem with Create after using it. But you can't call the update, here's the SQL that you get after the code runs
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type BaseModel struct {
Updated int64 `gorm:"autoUpdateTime;serializer:unixtime"` // Use unix MILLI seconds as updating time
Created int64 `gorm:"autoCreateTime;serializer:unixtime"` // Use unix seconds as creating time
}
func main() {
dsn := "root:123456@tcp(127.0.0.1:3306)/?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
createSql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Create(&BaseModel{})
})
fmt.Println("createSql", createSql)
updateSql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Updates(&BaseModel{})
})
fmt.Println("updateSql", updateSql)
}
Here are the results
createSql INSERT INTO `base_models` (`updated`,`created`) VALUES ('2024-04-29 16:03:24','2024-04-29 16:03:24')
2024/04/30 00:03:24 E:/demo/main.go:26 WHERE conditions required
updateSql UPDATE `base_models` SET `updated`=1714406604
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: gg1229505432
Try this:
type BaseModel struct {
Updated int64 `gorm:"autoUpdateTime;serializer:unixtime"`
Created int64 `gorm:"autoCreateTime;serializer:unixtime;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨