mirror of https://github.com/go-gorm/gorm.git
Add error explanations when preloading assocations w/o foreign fields, close #4356
This commit is contained in:
parent
b13732c450
commit
52b72d7ef2
|
@ -1,6 +1,7 @@
|
|||
package callbacks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
@ -144,7 +145,8 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
|||
fieldValues[idx], _ = field.ValueOf(elem)
|
||||
}
|
||||
|
||||
for _, data := range identityMap[utils.ToStringKey(fieldValues...)] {
|
||||
if datas, ok := identityMap[utils.ToStringKey(fieldValues...)]; ok {
|
||||
for _, data := range datas {
|
||||
reflectFieldValue := rel.Field.ReflectValueOf(data)
|
||||
if reflectFieldValue.Kind() == reflect.Ptr && reflectFieldValue.IsNil() {
|
||||
reflectFieldValue.Set(reflect.New(rel.Field.FieldType.Elem()))
|
||||
|
@ -162,5 +164,8 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
|||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
db.AddError(fmt.Errorf("failed to assign association %#v, make sure foreign fields exists", elem.Interface()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue