我如何执行嵌套删除 gorm Golang?
How can I perform nested deletes gorm Golang?
我有 table 位有汽车的客户,而汽车有服务。
当我删除汽车时,我还想从数据库中删除服务
当我删除客户时,我希望能够删除客户及其汽车和服务。
我尝试了以下方法来删除与汽车相关的汽车和服务,但它只从数据库中删除了汽车,我该如何同时删除汽车及其服务?
type Customer struct {
gorm.Model
FirstName string
LastName string
Phone string `gorm:"typevarchar(100);unique_index"`
Cars []Car `gorm:"constraint:OnDelete:CASCADE;"`
}
type Car struct {
gorm.Model
Make string
Modelo string
Color string
VinNumber string `gorm:"typevarchar(100);unique_index"`
Services []*Service `gorm:"constraint:OnDelete:CASCADE;"`
CustomerId int
}
type Service struct {
gorm.Model
Comment string
Miles string
CarId int
}
//endpoints
//delete customer
func deleteCustomer(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
var customer Customer
db.First(&customer, params["id"])
db.Unscoped().Delete(&customer)
json.NewEncoder(w).Encode(&customer)
}
func deleteCar(w http.ResponseWriter, r *http.Request) {
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
params := mux.Vars(r)
var car Car
db.First(&car, params["id"])
db.Select("Services").Unscoped().Delete(&car)
json.NewEncoder(w).Encode(&car)
}
我已经尝试了我在 Whosebug 和文档中看到的大部分内容,但到目前为止似乎没有任何效果。
阅读文档和一堆 Whosebug 答案后,对我来说没有任何用处。文档 and/or Whosebug 建议我在迁移模型时添加约束,or/and 将约束添加到结构 gorm:"constraint:OnDelete:CASCADE;"
,我也试过 db.Select("cars").Delete(&customers)
但没有成功。这就是我的处理方式,不确定这是否是最佳做法,但它会从数据库中删除其相关记录。
我像这样更新了 deleteCustomer 和 deleteCar 端点:
//delete customer
func deleteCustomer(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
var customer Customer
var cars []Car
db.First(&customer, params["id"])
db.Model(&customer).Related(&cars)
deleteServicesSqlStatement := `
DELETE FROM services
WHERE car_id = ;`
for i, car := range cars {
fmt.Println(i, car.ID)
err := db.Exec(deleteServicesSqlStatement, car.ID).Error
if err != nil {
fmt.Println(err)
}
}
DeleteCarsSqlStatement := `
DELETE FROM cars
WHERE customer_id = ;`
err := db.Exec(DeleteCarsSqlStatement, params["id"]).Error
if err != nil {
fmt.Println(err)
}
db.Debug().Unscoped().Delete(&customer)
json.NewEncoder(w).Encode(&customer)
}
//delete car
func deleteCar(w http.ResponseWriter, r *http.Request) {
//handle CORS
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
params := mux.Vars(r)
var car Car
db.First(&car, params["id"])
sqlStatement := `
DELETE FROM services
WHERE car_id = ;`
err := db.Exec(sqlStatement, params["id"]).Error
if err != nil {
fmt.Println(err)
}
db.Debug().Unscoped().Delete(&car)
json.NewEncoder(w).Encode(&car)
}
我有 table 位有汽车的客户,而汽车有服务。 当我删除汽车时,我还想从数据库中删除服务 当我删除客户时,我希望能够删除客户及其汽车和服务。
我尝试了以下方法来删除与汽车相关的汽车和服务,但它只从数据库中删除了汽车,我该如何同时删除汽车及其服务?
type Customer struct {
gorm.Model
FirstName string
LastName string
Phone string `gorm:"typevarchar(100);unique_index"`
Cars []Car `gorm:"constraint:OnDelete:CASCADE;"`
}
type Car struct {
gorm.Model
Make string
Modelo string
Color string
VinNumber string `gorm:"typevarchar(100);unique_index"`
Services []*Service `gorm:"constraint:OnDelete:CASCADE;"`
CustomerId int
}
type Service struct {
gorm.Model
Comment string
Miles string
CarId int
}
//endpoints
//delete customer
func deleteCustomer(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
var customer Customer
db.First(&customer, params["id"])
db.Unscoped().Delete(&customer)
json.NewEncoder(w).Encode(&customer)
}
func deleteCar(w http.ResponseWriter, r *http.Request) {
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
params := mux.Vars(r)
var car Car
db.First(&car, params["id"])
db.Select("Services").Unscoped().Delete(&car)
json.NewEncoder(w).Encode(&car)
}
我已经尝试了我在 Whosebug 和文档中看到的大部分内容,但到目前为止似乎没有任何效果。
阅读文档和一堆 Whosebug 答案后,对我来说没有任何用处。文档 and/or Whosebug 建议我在迁移模型时添加约束,or/and 将约束添加到结构 gorm:"constraint:OnDelete:CASCADE;"
,我也试过 db.Select("cars").Delete(&customers)
但没有成功。这就是我的处理方式,不确定这是否是最佳做法,但它会从数据库中删除其相关记录。
我像这样更新了 deleteCustomer 和 deleteCar 端点:
//delete customer
func deleteCustomer(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
var customer Customer
var cars []Car
db.First(&customer, params["id"])
db.Model(&customer).Related(&cars)
deleteServicesSqlStatement := `
DELETE FROM services
WHERE car_id = ;`
for i, car := range cars {
fmt.Println(i, car.ID)
err := db.Exec(deleteServicesSqlStatement, car.ID).Error
if err != nil {
fmt.Println(err)
}
}
DeleteCarsSqlStatement := `
DELETE FROM cars
WHERE customer_id = ;`
err := db.Exec(DeleteCarsSqlStatement, params["id"]).Error
if err != nil {
fmt.Println(err)
}
db.Debug().Unscoped().Delete(&customer)
json.NewEncoder(w).Encode(&customer)
}
//delete car
func deleteCar(w http.ResponseWriter, r *http.Request) {
//handle CORS
setupResponse(&w, r)
if (*r).Method == "OPTIONS" {
return
}
params := mux.Vars(r)
var car Car
db.First(&car, params["id"])
sqlStatement := `
DELETE FROM services
WHERE car_id = ;`
err := db.Exec(sqlStatement, params["id"]).Error
if err != nil {
fmt.Println(err)
}
db.Debug().Unscoped().Delete(&car)
json.NewEncoder(w).Encode(&car)
}