Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gorm update assignment time field error #182

Closed
shuqingzai opened this issue Aug 28, 2023 · 8 comments
Closed

gorm update assignment time field error #182

shuqingzai opened this issue Aug 28, 2023 · 8 comments
Labels
Bug Report a reproducible bug or regression

Comments

@shuqingzai
Copy link

shuqingzai commented Aug 28, 2023

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
@shuqingzai shuqingzai added the Bug Report a reproducible bug or regression label Aug 28, 2023
@Issues-translate-bot Issues-translate-bot changed the title gorm 更新赋值时间字段错误 gorm update assignment time field error Aug 28, 2023
@gouguoyin
Copy link
Collaborator

What is the object after instantiation of CdnRefreshReq?

@shuqingzai
Copy link
Author

What is the object after instantiation of CdnRefreshReq?

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

@gouguoyin
Copy link
Collaborator

Fixed in v2.2.8

@dadianzi
Copy link

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

@gouguoyin
Copy link
Collaborator

How gmt_modified is defined?

@dadianzi
Copy link

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 定义可正常

@Issues-translate-bot
Copy link
Collaborator

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

@gouguoyin
Copy link
Collaborator

gouguoyin commented Sep 26, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report a reproducible bug or regression
Projects
None yet
Development

No branches or pull requests

4 participants