Your Question
Why does UpdatedAt get set during a Create, and how can I disable UpdatedAt being set just for a Create
CREATE TABLE examples (
example varchar(255) not null ,
created_at timestamp not null ,
updated_at timestamp
)
func TestUpdatedAtValueSetDuringCreate(t *testing.T) {
type Example struct {
Example string `gorm:"type:varchar(255);column:example;not null"`
CreatedAt sql.NullTime `gorm:"type:timestamp;column:created_at;not null"`
UpdatedAt sql.NullTime `gorm:"type:timestamp;column:updated_at"`
}
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Info, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: true, // Disable color
},
)
gdb, err := gorm.Open(postgres.Open("host=localhost port=5432 user=postgres password=postgres dbname=postgres"), &gorm.Config{Logger: newLogger})
if err != nil {
t.Fatalf("unable to create connection: %s", err)
}
err = gdb.Create(&Example{Example: "hello world"}).Error
if err != nil {
t.Fatalf("unable to create example: %s", err)
}
}
2023/01/26 17:50:11 /examples/db_test.go:79
[3.674ms] [rows:1] INSERT INTO "examples" ("example","created_at","updated_at") VALUES ('hello world','2023-01-26 17:50:11.457','2023-01-26 17:50:11.457')
--- PASS: TestUpdatedAtValueSetDuringCreate (0.01s)
PASS
The document you expected this should be explained
https://gorm.io/docs/conventions.html#Timestamp-Tracking
Expected answer
This is potentially a bug, or missing documentation/feature.
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days