Gorm: Error 1292 on update a row

Created on 3 Aug 2015  路  5Comments  路  Source: go-gorm/gorm

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?

question

Most helpful comment

how to diable the NO_ZERO_DATE in gorm?

All 5 comments

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_DATE in gorm?

select @@GLOBAL.sql_mode;

remove NO_ZERO_DATE from select result and paste to sql_mode value below(replace .....)

SET GLOBAL sql_mode = '.....'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

izouxv picture izouxv  路  3Comments

Ganitzsh picture Ganitzsh  路  3Comments

satb picture satb  路  3Comments

koalacxr picture koalacxr  路  3Comments

Quentin-M picture Quentin-M  路  3Comments