GORM更新查询在where条件下自动分配时间列
GORM update query automatically assign time column in where condition
我是 golang 的新手,我的结构如下所示
type User struct{
ID int `gorm:"column:ID;primary_key:auto_increment" json:"ID"`
Name *string `gorm:"column:Name;default:null" json:"Name"`
DeletedAt *time.Time `gorm:"column:DeletedAt;default:null" json:"DeletedAt"`
}
我在 go 中的更新查询如下
if err := database.GetMysqlDB().Debug().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
fmt.Println(err)
}
}
但是我的Mysql调试如下
UPDATE User SET <data> WHERE DelatedAt IS NULL and ID = 15
不明白为什么查询在where条件中添加了DeletedAt列?
备注
我的 table 名称和列都是大写的
根据来自 this link 的信息,当 DeleteAt *time.Time
添加到任何结构时,将为匹配的数据库 table 启用软删除功能。这就是为什么您使用 User
模型的所有查询都将包含 WHERE DeletedAt IS NULL
条件。
要解决这个问题,您可以使用 Unscoped
方法,该方法将在您的 SQL 查询中包含软删除的记录。
if err := database.GetMysqlDB().Debug().Unscoped().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
fmt.Println(err)
}
}
我是 golang 的新手,我的结构如下所示
type User struct{
ID int `gorm:"column:ID;primary_key:auto_increment" json:"ID"`
Name *string `gorm:"column:Name;default:null" json:"Name"`
DeletedAt *time.Time `gorm:"column:DeletedAt;default:null" json:"DeletedAt"`
}
我在 go 中的更新查询如下
if err := database.GetMysqlDB().Debug().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
fmt.Println(err)
}
}
但是我的Mysql调试如下
UPDATE User SET <data> WHERE DelatedAt IS NULL and ID = 15
不明白为什么查询在where条件中添加了DeletedAt列?
备注 我的 table 名称和列都是大写的
根据来自 this link 的信息,当 DeleteAt *time.Time
添加到任何结构时,将为匹配的数据库 table 启用软删除功能。这就是为什么您使用 User
模型的所有查询都将包含 WHERE DeletedAt IS NULL
条件。
要解决这个问题,您可以使用 Unscoped
方法,该方法将在您的 SQL 查询中包含软删除的记录。
if err := database.GetMysqlDB().Debug().Unscoped().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
fmt.Println(err)
}
}