Gorm 更新

func TestUpdate(t *testing.T) {
	time := time.Now()
	user := User{
		Name:     "tiecheng",
		Age:      19,
		Birthday: &time,
	}

	DB.Model(&user).Where("name = ?", user.Name).Update("nickname", "鐵手")
}

输出 SQL

UPDATE `gdcloud_user` SET `nickname`="鐵手",`updated_at`="2020-09-10 20:20:18.147" WHERE name = "tiecheng"

如果对象改成

type User struct {
	models.Base
	Name     string
	Age      uint
	Birthday *time.Time
	Nickname *string
	Address  string `gorm:"default:hangzhou"`
}

输出 SQL

一口老血,在 gorm v1 的时候是正常的时间,而 v2 就变成时间戳了

唯一的区别在于 type:datetime 这个类型

去掉这个类型后,输出 SQL 如下:

最后更新于

这有帮助吗?