You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

package gormExample
import (
"fmt"
"log"
)
func DeleteWhere() {
result1 := DB.Delete(&Content{}, "likes < ?", 100)
if result1.Error != nil {
log.Fatalln(result1.Error)
}
//[9.643ms] [rows:0] UPDATE `msb_content` SET `deleted_at`='2023-04-21 19:11:47.799' WHERE likes < 100 AND `msb_content`.`deleted_at` IS NULL
result2 := DB.Where("likes < ?", 100).Delete(&Content{})
if result2.Error != nil {
log.Fatalln(result2.Error)
}
//[5.928ms] [rows:0] UPDATE `msb_content` SET `deleted_at`='2023-04-21 19:11:47.807' WHERE likes < 100 AND `msb_content`.`deleted_at` IS NULL
}
func FindDeleted() {
var c Content
DB.Delete(&c, 13)
if err := DB.First(&c, 13).Error; err != nil {
log.Println(err)
}
//[4.604ms] [rows:0] SELECT * FROM `msb_content` WHERE `msb_content`.`id` = 13 AND `msb_content`.`deleted_at` IS NULL ORDER BY `msb_content`.`id` LIMIT 1
if err := DB.Unscoped().First(&c, 13).Error; err != nil {
log.Println(err)
}
// [3.320ms] [rows:1] SELECT * FROM `msb_content` WHERE `msb_content`.`id` = 13 ORDER BY `msb_content`.`id` LIMIT 1
fmt.Printf("%+v\n", c)
}
func DeleteHard() {
var c Content
if err := DB.Unscoped().Delete(&c, 14).Error; err != nil {
log.Fatalln(err)
}
// [8.135ms] [rows:0] DELETE FROM `msb_content` WHERE `msb_content`.`id` = 14
}