Gorm: CreateAt, UpdateAt, DeleteAt

Created on 25 May 2017  Â·  6Comments  Â·  Source: go-gorm/gorm

Before posting a bug report about a problem, please try to verify that it is a bug and that it has not been reported already, please apply corresponding GitHub labels to the issue, for feature requests, please apply type:feature.

DON'T post usage related questions, ask in https://gitter.im/jinzhu/gorm or http://stackoverflow.com/questions/tagged/go-gorm,

Please answer these questions before submitting your issue. Thanks!

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

1.8

Which database and its version are you using?

mysql

What did you do?

when I define the gorm.Model like

{
ID uint64 `gorm:"primary_key"`
CreaetAt uint64 `gorm:"type:timestamp"`
UpdateAt uint64 `gorm:"type:timestamp"`
DeleteAt *time.Time `gorm:"index"`
}

but in mysql

show create table

  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `deleted_at` timestamp NULL DEFAULT NULL,

I think it should be

  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` timestamp NULL DEFAULT NULL,

Most helpful comment

@Wojtechnology @YJinHai I have solved this problem locally. gorm seems to be very specific about format and spaces when defining defaults:

type Model struct {
       CreatedAt time.Time  `json:"createdAt" gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP"`
       UpdatedAt time.Time  `json:"updatedAt" gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`
}

Note how i define the default. "default:\ If I don't define a default for created_at will be set to "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" which is the issue @Wojtechnology is talking about.

All 6 comments

I have the same problem,

my struct column as below:

Created uint64 `gorm:"type:timestamp" json:"created"`
Updated uint64 `gorm:"type:timestamp" json:"updated"`

error log as below:
(Error 1067: Invalid default value for 'updated')

please resolve it!

Should use time.Time for timestamp fields.

This is still an issue. updated_at should have the ON UPDATE CURRENT_TIMESTAMP, whereas created_at has it.

It's still a problem

@jinzhu

@Wojtechnology @YJinHai I have solved this problem locally. gorm seems to be very specific about format and spaces when defining defaults:

type Model struct {
       CreatedAt time.Time  `json:"createdAt" gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP"`
       UpdatedAt time.Time  `json:"updatedAt" gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`
}

Note how i define the default. "default:\ If I don't define a default for created_at will be set to "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" which is the issue @Wojtechnology is talking about.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

izouxv picture izouxv  Â·  3Comments

youtwo123 picture youtwo123  Â·  3Comments

koalacxr picture koalacxr  Â·  3Comments

sredxny picture sredxny  Â·  3Comments

Ganitzsh picture Ganitzsh  Â·  3Comments