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) + } } } }