forked from mirror/gorm
Don't run auto migrate if join table doesn't exist
This commit is contained in:
parent
7e587724e8
commit
dbedca4e5f
7
main.go
7
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue