Gorm用于批量删除

huangapple go评论78阅读模式
英文:

Gorm for batch deletion

问题

Golang使用Gorm框架进行批量删除。如何编写这个语句?

func DeleteUsers(id []int64) error {
	return db.Table("users").Where("id IN (?)", id).Delete(&User{}).Error
}

使用上述方法后,表中的所有数据将被清除。

英文:

Golang uses the Gorm framework for batch deletion. How to write this statement?

func DeleteUsers(id []int64) error {
	return db.Table("users").Delete(id).Error
}

After using the above method, all the data in the table will be cleared.

答案1

得分: 0

你可以按照以下方式更新代码以解决问题。

func DeleteUsers(id []int64) error {
    return db.Table("users").Where("users.id", id).Delete(struct{}{}).Error
}
英文:

You can update the code as below to solve the issue.

func DeleteUsers(id []int64) error {
    return db.Table("users").Where("users.id", id).Delete(struct{}{}).Error
}

huangapple
  • 本文由 发表于 2022年2月20日 20:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71194650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定