我正在使用gorm并具有如下模型:
type MyModel struct {
CreationTime time.Time
UpdateTime time.Time
}
我意识到在部署数据库之后,创建时间和更新时间并没有增加创建和更新项目的时间!我试图通过将模型更新为以下内容来解决此问题:type MyModel struct {
CreationTime time.Time `gorm:"default:current_timestamp"`
UpdateTime time.Time `gorm:"default:current_timestamp ON update current_timestamp"`
}
但是,像下面这样的自动迁移不会将这些默认值应用于已经存在的表! db.AutoMigrate(&MyModel{})
有什么方法可以使用内置的迁移功能向列添加默认值?我搜索了文档,似乎只能更改现有列的类型!如果不是,为什么要通过迁移将默认值简单地添加到列的任何建议?
最佳答案
您是否尝试过sql
标签?
`sql:"DEFAULT:current_timestamp"`
关于postgresql - 如何使用Gorm向现有列添加默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63105921/