forked from mirror/gorm
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
|
package callbacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -144,7 +145,8 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||||
fieldValues[idx], _ = field.ValueOf(elem)
|
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)
|
reflectFieldValue := rel.Field.ReflectValueOf(data)
|
||||||
if reflectFieldValue.Kind() == reflect.Ptr && reflectFieldValue.IsNil() {
|
if reflectFieldValue.Kind() == reflect.Ptr && reflectFieldValue.IsNil() {
|
||||||
reflectFieldValue.Set(reflect.New(rel.Field.FieldType.Elem()))
|
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