Gorm: CreatedAt is type of int64,when i update the col `created_at`, it switch into a string of time! Why

Created on 4 Jan 2019  Â·  8Comments  Â·  Source: go-gorm/gorm

Your issue may already be reported! Please search on the issue track before creating one.

What version of Go are you using (go version)?

go version go1.9.6 windows/amd64

Which database and its version are you using?

mysql

Please provide a complete runnable program to reproduce your issue. IMPORTANT

type ConfigRoute struct {
    RouteId        int64 `gorm:"PRIMARY_KEY"`
    NodeId         string
    Endpoint       string
    StartAccountId int64
    EndAccountId   int64
    CreatedAt      int64
    UpdatedAt      int64
    StartAt        int64
    ExpireAt       int64
    Disabled       bool
}

func (route *ConfigRoute) UpdateDisabled() error {
    nowMs := 1546568283096
    ret := db.Model(route).Updates(map[string]interface{}{
        "disabled":   route.Disabled,
        "updated_at": nowMs, // but if using int(nowMs),it would be ok.
    })
    if err := ret.Error; err != nil {
        return err
    }
    return nil
}

func main(){
 // initialize db connection.
   route := &ConfigRoute {
      /....
   }
   route.Save()
   route.Disabled = false
   route.UpdateDisabled() // err occurs!!!
}
gorm_v1

Most helpful comment

created_at, updated_at are the default time type in gorm, and will set to time.now when create/update.
you can remove the callback after the db init

db.Callback().Create().Remove("gorm:update_time_stamp")
db.Callback().Update().Remove("gorm:update_time_stamp")

All 8 comments

There is no statement that i want the updated_at to be seem like a type of datetime, but why the pkg make me do that?

Here is the log lines:

(D:/src/cryptobroker/trademaster/dto/route.go:55) 
[2019-01-04 10:18:05]  Error 1265: Data truncated for column 'updated_at' at row 1 

(D:/src/cryptobroker/trademaster/dto/route.go:55) 
[2019-01-04 10:18:05]  [1919.86ms]  UPDATE `config_routes` SET `disabled` = 'true', `updated_at` = '2019-01-04 10:18:04'  WHERE `config_routes`.`route_id` = '1'  
[0 rows affected or returned ] 

I couldn't reproduce the problem with the code you posted, but I believe that this problem can be solved with a slight change in your code.

Rather than dealing with update timestamps, let Gorm deal with them for you by adding gorm.Model to your structure.

Change your structure from what you posted to:

type ConfigRoute struct {
        gorm.Model
    NodeId         string
    Endpoint       string
    StartAccountId int64
    EndAccountId   int64
    StartAt        int64
    ExpireAt       int64
    Disabled       bool
}

Note that this will also add a primary key ID. You can find more information here.

If you want to keep your current approach can you maybe provide the smallest program you can which reproduces this bug? Then I would gladly take a look at it 💯

Having reviewed the code, i could see that the field update_at having been replace from int64 to datetime like 2006-01-02 2015:04:05

Same issue.

Model:

type Post struct {
    ...
    UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;type:intenger"`
    ...
}

Method:

func (db *DB) UpdatePost(src *models.Post, changedOnly bool) (post *models.Post, err error) {
    now := time.Now().UTC().Unix()
    src.UpdatedAt = now

    q := db.DB.Model(&models.Post{})
    if changedOnly {
        err = q.Update(src).Error
    } else {
        err = q.Save(src).Error
    }

    return src, err
}

Result:

...
[2019-01-24 19:46:17]  sql: Scan error on column index 11, name "updated_at": converting driver.Value type []uint8 ("2019-01-24 19:46:17.339375736+05:00") to a int64: invalid syntax 
...
.../internal/db/posts.go:63) 
[2019-01-24 19:57:22]  [11.55ms]  UPDATE "posts" SET ... "updated_at" = '2019-01-24 19:57:22' ...
/internal/db/posts.go:37) 

Why? :confused:

created_at, updated_at are the default time type in gorm, and will set to time.now when create/update.
you can remove the callback after the db init

db.Callback().Create().Remove("gorm:update_time_stamp")
db.Callback().Update().Remove("gorm:update_time_stamp")

saved my life!

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

easonlin404 picture easonlin404  Â·  3Comments

corvinusy picture corvinusy  Â·  3Comments

Quentin-M picture Quentin-M  Â·  3Comments

youtwo123 picture youtwo123  Â·  3Comments

superwf picture superwf  Â·  3Comments