删除
1 删除
DB.Delete(&book)
func deleteBook(request *gin.Context) {
id := request.Param("id")
var book Book
DB.Model(&book).Where("id=?", id)
DB.Delete(&book)
request.JSON(200, "success")
}
如果是多个数据时,批量删除
批量删除必须要有条件
2 钩子函数
BeforeDelete
、AfterDelete
func (u *Book) BeforeDelete(tx *gorm.DB) (err error) { // 删除book数据之前自动调用
if u.Role == "admin" {
return errors.New("admin user not allowed to delete")
}
return
}