Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GORM更新查询在where条件下自动分配时间列_Go_Go Gorm - Fatal编程技术网

GORM更新查询在where条件下自动分配时间列

GORM更新查询在where条件下自动分配时间列,go,go-gorm,Go,Go Gorm,我是刚到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:

我是刚到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"`
}
UPDATE User SET <data> WHERE DelatedAt IS NULL and ID = 15
我的更新查询如下所示

if err := database.GetMysqlDB().Debug().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
        fmt.Println(err)
    }
}
但我的Mysql调试如下所示

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"`
}
UPDATE User SET <data> WHERE DelatedAt IS NULL and ID = 15
更新DelatedAt为NULL且ID=15的用户集
我不明白为什么查询会在where条件中添加DeletedAt列

注意 我的表名和列都是大写的,当
DeleteAt*time.time
添加到任何结构中时,将为匹配的数据库表启用软删除功能。这就是为什么对
User
模型的所有查询都会包含
WHERE DeletedAt为NULL
条件的原因

为了解决这个问题,您可以使用
Unscoped
方法,该方法将在SQL查询中包含软删除的记录

if err := database.GetMysqlDB().Debug().Unscoped().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
        fmt.Println(err)
    }
}
根据链接中的信息,当将
DeleteAt*time.time
添加到任何结构中时,将为匹配的数据库表启用软删除功能。这就是为什么对
User
模型的所有查询都会包含
WHERE DeletedAt为NULL
条件的原因

为了解决这个问题,您可以使用
Unscoped
方法,该方法将在SQL查询中包含软删除的记录

if err := database.GetMysqlDB().Debug().Unscoped().Model(&User{}).Where("ID = ?", 15).UpdateColumns(user).Error; err != nil {
        fmt.Println(err)
    }
}

这是因为当表中的
DeleteAt
列可用时,会启用软删除选项。这是因为当表中的
DeleteAt
列可用时,会启用软删除选项。