From dbedca4e5fe81ba68969835399c3d0866a260fa3 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 23 Jun 2015 14:19:59 +0800 Subject: [PATCH] Don't run auto migrate if join table doesn't exist --- main.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 181722fd..c3a1629c 100644 --- a/main.go +++ b/main.go @@ -473,14 +473,17 @@ func (s *DB) Get(name string) (value interface{}, ok bool) { } func (s *DB) SetJoinTableHandler(source interface{}, column string, handler JoinTableHandlerInterface) { - for _, field := range s.NewScope(source).GetModelStruct().StructFields { + scope := s.NewScope(source) + for _, field := range scope.GetModelStruct().StructFields { if field.Name == column || field.DBName == column { if many2many := parseTagSetting(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" { source := (&Scope{Value: source}).GetModelStruct().ModelType destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType handler.Setup(field.Relationship, many2many, source, destination) field.Relationship.JoinTableHandler = handler - s.Table(handler.Table(s)).AutoMigrate(handler) + if table := handler.Table(s); scope.Dialect().HasTable(scope, table) { + s.Table(table).AutoMigrate(handler) + } } } }