Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "github.com/jinzhu/gorm"
- _ "github.com/lib/pq"
- //"fmt"
- "log"
- )
- // has_many Ys
- type X struct {
- gorm.Model
- }
- // belongs_to X
- type Y struct {
- gorm.Model
- XID uint
- }
- func main() {
- db, err := gorm.Open("postgres", "postgres://user:psw@localhost/db")
- if err != nil {
- log.Fatal(err)
- }
- defer db.Close()
- db.LogMode(true)
- db.AutoMigrate(X{}, Y{})
- db.Model(&Y{}).AddForeignKey("x_id", "xes (id)", "CASCADE", "CASCADE")
- }
- /*
- ===================== Вот такой лог ======================
- (/home/anoobis/pro/gorm/main.go:29)
- [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"))
- (/home/anoobis/pro/gorm/main.go:29)
- [2016-03-23 00:13:06] [88.58ms] CREATE INDEX idx_xes_deleted_at ON "xes"("deleted_at")
- (/home/anoobis/pro/gorm/main.go:29)
- [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"))
- (/home/anoobis/pro/gorm/main.go:29)
- [2016-03-23 00:13:07] [92.48ms] CREATE INDEX idx_ys_deleted_at ON "ys"("deleted_at")
- (/home/anoobis/pro/gorm/main.go:30)
- [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;
- ===================== собственно ==========================
- ALTER TABLE "ys" ADD CONSTRAINT "ys_x_id_xes_id_foreign" FOREIGN KEY ("x_id") REFERENCES xes (id) ON DELETE CASCADE ON UPDATE CASCADE;
- И есть добавление ограничения внешнего ключа к x_id (таблици ys) - так чтоб он полюбому соответствовал какому-либо
- существующему id в таблице xes.
- =================== в psql =========================
- \d xes
- Table "public.xes"
- Column | Type | Modifiers
- ------------+--------------------------+--------------------------------------------------
- id | integer | not null default nextval('xes_id_seq'::regclass)
- created_at | timestamp with time zone |
- updated_at | timestamp with time zone |
- deleted_at | timestamp with time zone |
- Indexes:
- "xes_pkey" PRIMARY KEY, btree (id)
- "idx_xes_deleted_at" btree (deleted_at)
- Referenced by:
- TABLE "ys" CONSTRAINT "ys_x_id_xes_id_foreign" FOREIGN KEY (x_id) REFERENCES xes(id) ON UPDATE CASCADE ON DELETE CASCADE
- ====================== и ==================
- \d ys
- Table "public.ys"
- Column | Type | Modifiers
- ------------+--------------------------+-------------------------------------------------
- id | integer | not null default nextval('ys_id_seq'::regclass)
- created_at | timestamp with time zone |
- updated_at | timestamp with time zone |
- deleted_at | timestamp with time zone |
- x_id | integer |
- Indexes:
- "ys_pkey" PRIMARY KEY, btree (id)
- "idx_ys_deleted_at" btree (deleted_at)
- Foreign-key constraints:
- "ys_x_id_xes_id_foreign" FOREIGN KEY (x_id) REFERENCES xes(id) ON UPDATE CASCADE ON DELETE CASCADE
- =================================== проверка ограничения ======================
- INSERT INTO ys (created_at, x_id) VALUES (now(), 90);
- ERROR: insert or update on table "ys" violates foreign key constraint "ys_x_id_xes_id_foreign"
- DETAIL: Key (x_id)=(90) is not present in table "xes".
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement