Gorm 赋空值

Nov 27, 2023

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)

Alluka

一只坐井观天的菜狗子