Use existing value if has
@@ -197,9 +197,12 @@ func (meta *Meta) Initialize() error {
}
if f, ok := scope.FieldByName(fieldName); ok {
- if f.Relationship != nil && f.Field.CanAddr() && !scope.PrimaryKeyZero() {
+ if f.Relationship != nil && f.Field.CanAddr() {
+ if ((relationship.Kind == "has_many" || relationship.Kind == "many_to_many") && f.Field.Len() == 0) ||
+ ((relationship.Kind == "has_one" || relationship.Kind == "belongs_to") && scope.PrimaryKeyZero()) {
context.GetDB().Model(value).Related(f.Field.Addr().Interface(), meta.FieldName)
}
+ }
return f.Field.Interface()
}
