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

autoUpdateTime and default seem to be conflicting #7281

Open
diwuwudi123 opened this issue Nov 16, 2024 · 2 comments
Open

autoUpdateTime and default seem to be conflicting #7281

diwuwudi123 opened this issue Nov 16, 2024 · 2 comments
Assignees
Labels

Comments

@diwuwudi123
Copy link

diwuwudi123 commented Nov 16, 2024

autoUpdateTime and default seem to be conflicting

As defined in the gorm documentation. I set the default value of these fields to 0 and auto-populate the current timestamp when creating or updating.
but it doesn't seem to work. If I remove the default values the logic of auto-populating the current timestamp will work.

`
package main

import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

type AutoTime struct {
ID int
Create_time int64 gorm:"autoCreateTime;default:0;not null"
Update_time int64 gorm:"autoUpdateTime;default:0;not null"
Delete_time int64 gorm:"default:0;not null"
}

func main() {
dsn := "root:root@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&AutoTime{})

pd := AutoTime{}
db.Create(&pd)

var result AutoTime
db.First(&result, pd.ID)
println("Create Time:", result.Create_time)
println("Update Time:", result.Update_time)

pd.Delete_time = 1
db.Save(&pd)

db.First(&result, pd.ID)
println("Update Time:", result.Update_time)

}

`

The document you expected this should be explained

In the above code I have created a data model and corresponding field definitions.
Then I inserted a piece of data, expecting both create_time and update_time to be the current timestamp.
However, after creation they are both 0, and only after updating are the values of update_time updated to the current timestamp.

Expected answer

I want both create_time and update_time to be the current timestamp after creation. Is there something wrong with my usage?

@diwuwudi123 diwuwudi123 added the type:question general questions label Nov 16, 2024
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Nov 16, 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

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

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

2 participants