DELETE USING in gorm?
Solution 1:
Here i have used Postgresql and used db.Raw() function to run DELETE FROM ... USING ... WHERE ...
command
I have created 2 table of customer and owner in postgresql inserted data manually using psql cli
Before running any Golang program
customer table :
owner table :
package main
import (
"log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type customer struct {
Id int `gorm:"id"`
First_name string `gorm:"first_name"`
Last_name string `gorm:"last_name"`
Email string `gorm:"email"`
Gender string `gorm"gender"`
Ip_address string `gorm:"ip_address"`
}
func main () {
dburl:= "postgres://postgres:password@localhost:5432"
db,err := gorm.Open(postgres.Open(dburl),&gorm.Config{})
if err !=nil {
log.Fatalln("error connnection")
}
// db.Exec("DELETE customer using owner WHERE customer.first_name = owner.first_name ")
var s customer
db.Raw("DELETE FROM customer using owner WHERE customer.first_name = owner.first_name ").Scan(&s)
}
After running this Golang code first_name field is deleted from customer table :