gorm系列-删除
程序员文章站
2022-03-26 19:48:17
[TOC] Gorm删除 软删除 删除记录 警告 删除记录时,请 确保主键字段有值 ,GORM 会通过主键去删除记录,如果主键为空,GORM 会删除该 model 的所有记录。 go package main import ( "github.com/jinzhu/gorm" _ "github.c ......
目录
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{}) }