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!
go version)?1.8
mysql
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,
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:\
Most helpful comment
@Wojtechnology @YJinHai I have solved this problem locally. gorm seems to be very specific about format and spaces when defining defaults:
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.