golang-module / carbon

A simple, semantic and developer-friendly golang package for time

Home Page:https://pkg.go.dev/github.com/golang-module/carbon/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gorm update assignment time field error

shuqingzai opened this issue · comments

2.2.5 之前更新,如果 updatedAtcarbon.DateTime 类型,时间会正确转换

type CdnRefreshReq struct {
	ID              uint64                       `gorm:"column:id;type:bigint(20) unsigned;primaryKey;comment:ID" json:"id,string" dc:"ID"`                                                                                                                                                      // ID
	CreatedAt       carbon.DateTime              `gorm:"column:createdAt;type:timestamp;not null;autoCreateTime;comment:创建时间" json:"createdAt" dc:"创建时间"`                                                                                                                                        // 创建时间
	UpdatedAt       carbon.DateTime              `gorm:"column:updatedAt;type:timestamp;not null;autoUpdateTime;comment:更新时间" json:"updatedAt" dc:"更新时间"`                                                                                                                                        // 更新时间
	DeletedAt       gorm.DeletedAt               `gorm:"column:deletedAt;type:timestamp;comment:删除时间" json:"deletedAt" dc:"删除时间"`                                                                                                                                                                // 删除时间
	Status          int32                        `gorm:"column:status;type:tinyint(3) unsigned;not null;comment:状态:0|pending=待处理,1|processing=处理中,2|processed=已处理" json:"status"`
}
UPDATE `cdn_refresh_req` SET `status`='1',`updatedAt`='2023-08-28 21:26:28.819' WHERE `id` = 16268780338438144 AND `cdn_refresh_req`.`deletedAt` IS NULL

同样的语句,在 2.2.5 报错,因为 updatedAt 转换为时间戳了

UPDATE `cdn_refresh_req` SET `status`='1',`updatedAt`=1693228949 WHERE `id` = 16267778449244160 AND `cdn_refresh_req`.`deletedAt` IS NULL

What is the object after instantiation of CdnRefreshReq?

What is the object after instantiation of CdnRefreshReq?

是的,简单的更新 status 的值而已

Fixed in v2.2.8

2.2.8版本还可以重现

UPDATE "biz_task" SET gmt_modified"=1695611775 WHERE id = 136;

go version go1.20.7 darwin/amd64

gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.4

How gmt_modified is defined?

How gmt_modified is defined?

这是目前定义的方式

type Base struct {
	GmtCreate   carbon.Carbon   `gorm:"autoCreateTime"`
	GmtModified carbon.Carbon `gorm:"autoUpdateTime"`
	Id          int             gorm:"primaryKey"
	IsDeleted   bool
}

经测试

type Base struct {
	GmtCreate   carbon.DateTime `gorm:"autoCreateTime"`
	GmtModified carbon.DateTime `gorm:"autoUpdateTime"`
	Id          int             `gorm:"primaryKey"`
	IsDeleted   bool
}

改为 carbon.DateTime 定义可正常

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


How gmt_modified is defined?

This is how it is currently defined

type Base struct {
GmtCreate carbon.Carbon `gorm:"autoCreateTime"`
GmtModified carbon.Carbon `gorm:"autoUpdateTime"`
Id int gorm:"primaryKey"
IsDeleted bool
}

tested

type Base struct {
GmtCreate carbon.DateTime `gorm:"autoCreateTime"`
GmtModified carbon.DateTime `gorm:"autoUpdateTime"`
Id int `gorm:"primaryKey"`
IsDeleted bool
}

Change the definition to carbon.DateTime and it will work normally