Hi
I define container struct
type Container struct {
ID string `gorm:"primary_key" sql:"type:varchar(64)"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
Image string `sql:"type:varchar(255);not null"`
Status string `sql:"type:varchar(255);not null"`
}
When i try to update row like this
db.Connect()
db.DbConnection.Model(&tables.Container{ID: id}).Update(tables.Container{Status: "running"})
I got error 1292
(Error 1292: Incorrect datetime value: '0000-00-00' for column 'created_at' at row 1)
Am i in a wrong way? why this happen?
Hi, this is because you are using mysql version 5.7 which doesn't allow that. https://www.digitalocean.com/community/tutorials/how-to-prepare-for-your-mysql-5-7-upgrade
You could disable the NO_ZERO_DATE in your sql mode.
select @@GLOBAL.sql_mode;
SET GLOBAL sql_mode = '.....'
how to diable the NO_ZERO_DATE in gorm?
I have not really tested this fully, but in a trivial example, this seems to work. Copy gorm.Model into your own copy, and change the time.Time to *time.Time. If the pointer is nil, gorm does the right thing and inserts NULL instead of inserting an invalid time.
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt *time.Time
UpdatedAt *time.Time
DeletedAt *time.Time `sql:"index"`
}
type User struct {
Model
Name string
Email string `gorm:"not null;unique_index"`
Color string
}
how to diable the
NO_ZERO_DATEin gorm?
select @@GLOBAL.sql_mode;
remove NO_ZERO_DATE from select result and paste to sql_mode value below(replace .....)
SET GLOBAL sql_mode = '.....'
Most helpful comment
how to diable the
NO_ZERO_DATEin gorm?