Don't run auto migrate if join table doesn't exist

This commit is contained in:
Jinzhu 2015-06-23 14:19:59 +08:00
parent 7e587724e8
commit dbedca4e5f
1 changed files with 5 additions and 2 deletions

View File

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