Advertisement
Guest User

только путь к баз замени на свой

a guest
Mar 22nd, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.79 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "github.com/jinzhu/gorm"
  5.     _ "github.com/lib/pq"
  6.  
  7.     //"fmt"
  8.     "log"
  9. )
  10.  
  11. // has_many Ys
  12. type X struct {
  13.     gorm.Model
  14. }
  15.  
  16. // belongs_to X
  17. type Y struct {
  18.     gorm.Model
  19.     XID uint
  20. }
  21.  
  22. func main() {
  23.     db, err := gorm.Open("postgres", "postgres://user:psw@localhost/db")
  24.     if err != nil {
  25.         log.Fatal(err)
  26.     }
  27.     defer db.Close()
  28.     db.LogMode(true)
  29.     db.AutoMigrate(X{}, Y{})
  30.     db.Model(&Y{}).AddForeignKey("x_id", "xes (id)", "CASCADE", "CASCADE")
  31. }
  32.  
  33. /*
  34.  
  35. =====================       Вот такой лог ======================
  36.  
  37.  
  38. (/home/anoobis/pro/gorm/main.go:29)
  39. [2016-03-23 00:13:06]  [157.16ms]  CREATE TABLE "xes" ("id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone , PRIMARY KEY ("id"))
  40.  
  41. (/home/anoobis/pro/gorm/main.go:29)
  42. [2016-03-23 00:13:06]  [88.58ms]  CREATE INDEX idx_xes_deleted_at ON "xes"("deleted_at")
  43.  
  44. (/home/anoobis/pro/gorm/main.go:29)
  45. [2016-03-23 00:13:07]  [111.14ms]  CREATE TABLE "ys" ("id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone,"x_id" integer , PRIMARY KEY ("id"))
  46.  
  47. (/home/anoobis/pro/gorm/main.go:29)
  48. [2016-03-23 00:13:07]  [92.48ms]  CREATE INDEX idx_ys_deleted_at ON "ys"("deleted_at")
  49.  
  50. (/home/anoobis/pro/gorm/main.go:30)
  51. [2016-03-23 00:13:07]  [142.14ms]  ALTER TABLE "ys" ADD CONSTRAINT "ys_x_id_xes_id_foreign" FOREIGN KEY ("x_id") REFERENCES xes (id) ON DELETE CASCADE ON UPDATE CASCADE;
  52.  
  53.  
  54. ===================== собственно ==========================
  55.  
  56. ALTER TABLE "ys" ADD CONSTRAINT "ys_x_id_xes_id_foreign" FOREIGN KEY ("x_id") REFERENCES xes (id) ON DELETE CASCADE ON UPDATE CASCADE;
  57.  
  58. И есть добавление ограничения внешнего ключа к x_id (таблици ys) - так чтоб он полюбому соответствовал какому-либо
  59. существующему id в таблице xes.
  60.  
  61.  
  62. =================== в psql =========================
  63.  
  64.  \d xes
  65.                                     Table "public.xes"
  66.    Column   |           Type           |                    Modifiers                    
  67. ------------+--------------------------+--------------------------------------------------
  68.  id         | integer                  | not null default nextval('xes_id_seq'::regclass)
  69.  created_at | timestamp with time zone |
  70.  updated_at | timestamp with time zone |
  71.  deleted_at | timestamp with time zone |
  72. Indexes:
  73.     "xes_pkey" PRIMARY KEY, btree (id)
  74.     "idx_xes_deleted_at" btree (deleted_at)
  75. Referenced by:
  76.     TABLE "ys" CONSTRAINT "ys_x_id_xes_id_foreign" FOREIGN KEY (x_id) REFERENCES xes(id) ON UPDATE CASCADE ON DELETE CASCADE
  77.  
  78. ====================== и ==================
  79.  
  80. \d ys
  81.                                     Table "public.ys"
  82.    Column   |           Type           |                    Modifiers                    
  83. ------------+--------------------------+-------------------------------------------------
  84.  id         | integer                  | not null default nextval('ys_id_seq'::regclass)
  85.  created_at | timestamp with time zone |
  86.  updated_at | timestamp with time zone |
  87.  deleted_at | timestamp with time zone |
  88.  x_id       | integer                  |
  89. Indexes:
  90.     "ys_pkey" PRIMARY KEY, btree (id)
  91.     "idx_ys_deleted_at" btree (deleted_at)
  92. Foreign-key constraints:
  93.     "ys_x_id_xes_id_foreign" FOREIGN KEY (x_id) REFERENCES xes(id) ON UPDATE CASCADE ON DELETE CASCADE
  94.  
  95.  
  96. =================================== проверка ограничения ======================
  97.  
  98. INSERT INTO ys (created_at, x_id) VALUES (now(), 90);
  99. ERROR:  insert or update on table "ys" violates foreign key constraint "ys_x_id_xes_id_foreign"
  100. DETAIL:  Key (x_id)=(90) is not present in table "xes".
  101.  
  102. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement