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

Serialization error on update #7001

Open
sjm1327605995 opened this issue Apr 29, 2024 · 3 comments
Open

Serialization error on update #7001

sjm1327605995 opened this issue Apr 29, 2024 · 3 comments
Assignees
Labels

Comments

@sjm1327605995
Copy link

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

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Apr 29, 2024
Copy link

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.ioSearch Before Asking

@gg1229505432
Copy link

gg1229505432 commented May 2, 2024

Try this:

type BaseModel struct {
	Updated int64 `gorm:"autoUpdateTime;serializer:unixtime"` 
	Created int64 `gorm:"autoCreateTime;serializer:unixtime;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`

Copy link

github-actions bot commented May 2, 2024

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.ioSearch Before Asking

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

No branches or pull requests

3 participants