Gorm删除
软删除
删除记录
警告 删除记录时,请确保主键字段有值,GORM 会通过主键去删除记录,如果主键为空,GORM 会删除该 model 的所有记录。
// 删除现有记录
db.Delete(&email)
//// DELETE from emails where id=10;
// 为删除 SQL 添加额外的 SQL 操作
db.Set("gorm:delete_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Delete(&email)
//// DELETE from emails where id=10 OPTION (OPTIMIZE FOR UNKNOWN);
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
//1. 定义模型
type User struct {
gorm.Model
Name string
Age byte
Active bool
}
func main() {
//2. 连接Mysql数据库
db, err := gorm.Open("mysql","root:123456@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer db.Close()
//3. 把模型与数据库中的表对应起来
db.AutoMigrate(&User{})
//4. 删除
var u User
u.ID = 1
db.Debug().Delete(u)
//[2020-04-27 16:35:12] [20.97ms] UPDATE `users` SET `deleted_at`='2020-04-27 16:35:12' WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 1
//[1 rows affected or returned ]
}
func main() {
//2. 连接Mysql数据库
db, err := gorm.Open("mysql","root:123456@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer db.Close()
//3. 把模型与数据库中的表对应起来
db.AutoMigrate(&User{})
//4. 删除
var u User
u.Name = "yike"
db.Debug().Delete(u)
//[2020-04-27 16:38:07] [20.97ms] UPDATE `users` SET `deleted_at`='2020-04-27 16:38:07' WHERE `users`.`deleted_at` IS NULL
//[1 rows affected or returned ]
}
批量删除
删除全部匹配的记录
db.Where("email LIKE ?", "%jinzhu%").Delete(Email{})
//// DELETE from emails where email LIKE "%jinzhu%";
db.Delete(Email{}, "email LIKE ?", "%jinzhu%")
//// DELETE from emails where email LIKE "%jinzhu%";
////4. 删除
//var u User
//u.Name = "yike"
//db.Debug().Delete(u)
db.Debug().Where("name=?","zisefeizhu").Delete(User{})
//db.Debug().Delete("age = ?", 21)
软删除
如果一个 model 有 DeletedAt 字段,他将自动获得软删除的功能! 当调用 Delete 方法时, 记录不会真正的从数据库中被删除, 只会将DeletedAt 字段的值会被设置为当前时间
db.Delete(&user)
//// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE id = 111;
// 批量删除
db.Where("age = ?", 20).Delete(&User{})
//// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE age = 20;
// 查询记录时会忽略被软删除的记录
db.Where("age = 20").Find(&user)
//// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;
// Unscoped 方法可以查询被软删除的记录
db.Unscoped().Where("age = 20").Find(&users)
//// SELECT * FROM users WHERE age = 20;
var u1 []User
db.Debug().Unscoped().Where("name = ?","yike").Find(&u1)
//[2020-04-27 16:49:05] [21.96ms] SELECT * FROM `users` WHERE (name = 'yike')
//[1 rows affected or returned ]
物理删除
//物理删除
//db.Debug().Unscoped().Where("name = ?", "zisefeizhu").Delete(&User{})
db.Debug().Unscoped().Where("name = ?", "yike").Delete(&User{})
不使用软删除
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
//1. 定义模型
type User struct {
//gorm.Model
ID int
Name string
Age byte
Active bool
}
func main() {
//2. 连接Mysql数据库
db, err := gorm.Open("mysql","root:123456@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer db.Close()
//3. 把模型与数据库中的表对应起来
db.AutoMigrate(&User{})
////创建记录
u1 := User{Name:"zisefeizhu", Age: 22, Active: true}
db.Create(&u1)
u2 := User{Name:" jingxing", Age: 21, Active:false}
db.Create(&u2)
}
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
//1. 定义模型
type User struct {
//gorm.Model
ID int
Name string
Age byte
Active bool
}
func main() {
//2. 连接Mysql数据库
db, err := gorm.Open("mysql","root:123456@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer db.Close()
//3. 把模型与数据库中的表对应起来
db.AutoMigrate(&User{})
//删除
db.Debug().Where("name=?","zisefeizhu").Delete(User{})
}
来源:oschina
链接:https://my.oschina.net/u/4414278/blog/4256883