Gorm 赋空值
Gorm中用assign直接对某个值做空值处理的时候会被跳过
type Person struct {
Name string
Age int
DeletedAt interface{}
}
var (
db *gorm.DB
person Person
)
db.Where(Person{Name: "Alex"}).
Assign(Person{
Age: 10,
DeletedAt: nil,
}).FirstOrCreate(&companyRecord)
用 gorm.Expr("NULL")
进行赋值,可将已经软删除(delete_at!=null)的数据重新恢复(delete_at=null)
type Person struct {
Name string
Age int
DeletedAt interface{}
}
var (
db *gorm.DB
person Person
)
db.Where(Person{Name: "Alex"}).
Assign(Person{
Age: 10,
DeletedAt: gorm.Expr("NULL"),
}).FirstOrCreate(&companyRecord)