Advertisement
nshibalov

qor_hasmany_hook_problem

Jan 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.89 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "log"
  5.     "net/http"
  6.  
  7.     "github.com/jinzhu/gorm"
  8.     "github.com/qor/admin"
  9.  
  10.     _ "github.com/jinzhu/gorm/dialects/sqlite"
  11. )
  12.  
  13. type Order struct {
  14.     gorm.Model
  15.     Name string
  16.  
  17.     OrderItems []OrderItem
  18. }
  19.  
  20. type OrderItem struct {
  21.     gorm.Model
  22.     Name string
  23.  
  24.     OrderID uint
  25. }
  26.  
  27. func (oi *OrderItem) BeforeDelete(tx *gorm.DB) error {
  28.     // There is a problem: oi.ID == 0
  29.     // if OrderItem deleted from http://localhost:9000/api/orders page
  30.     log.Println("BeforeDelete: ID:", oi.ID)
  31.     return nil
  32. }
  33.  
  34. func main() {
  35.     db, _ := gorm.Open("sqlite3", "demo.db")
  36.     db.AutoMigrate(&Order{}, &OrderItem{})
  37.  
  38.     adm := admin.New(&admin.AdminConfig{DB: db})
  39.     adm.AddResource(&Order{})
  40.     adm.AddResource(&OrderItem{})
  41.  
  42.     mux := http.NewServeMux()
  43.     adm.MountTo("/api", mux)
  44.  
  45.     serverAddress := ":9000"
  46.     log.Println("Listening on:", serverAddress)
  47.     http.ListenAndServe(serverAddress, mux)
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement