According to GORM's docs:
Updates supports update with struct or map[string]interface{}, when updating with struct it will only update non-zero fields by default
I have an entry in my database already for the Service with ID, abc123. I am attempting to take an object that looks like the one below:
Service{
ID: "abc123",
Name: "new service name",
CreatedAt: nil,
}
And use it to update the my existing record. But when I call:
tx.Model(&service).Updates(service)
the CreatedAt value in the database is overwritten with nil. How can I update my database record without overwritting the CreatedAt value?
Update: Below is my Service struct
type Service struct {
ID string `gorm:"not null;type:char(32);primary_key;column:id"`
Name string `json:"name" gorm:"size:50;not null;"`
CreatedAt *time.Time
UpdatedAt time.Time
DeletedAt *time.Time `gorm:"index"`
}
I've tried two different variations for my Service struct. the other is with CreatedAt being of type time.Time instead of *time.Time. With *time.Time it will overwrite the value in my DB with a null value. With time.Time it attempts to overwrite the value in the DB with an uninitialized time value and throws the error: Error 1292: Incorrect datetime value: '0000-00-00' for column 'created_at' at row 1
type Service struct {...}- bambulatime.Timefield istime.Time{}. Did you try that instead ofnil? - Emin LaletovicCreatedAtistime.TimethenCreatedAt: nil,will not compile, can you show the acutal code, it may be of importance. - mkopriva