forked from mirror/gorm
Fix set ForeignKey for embedded struct
This commit is contained in:
parent
6d13ae4ead
commit
cbcb88d3d1
|
@ -25,7 +25,7 @@ func SaveBeforeAssociations(scope *Scope) {
|
||||||
if !value.CanAddr() {
|
if !value.CanAddr() {
|
||||||
// If can't take address, then clone the value and set it back
|
// If can't take address, then clone the value and set it back
|
||||||
value = reflect.New(value.Type()).Elem()
|
value = reflect.New(value.Type()).Elem()
|
||||||
for _, f := range newDB.NewScope(field.Field.Interface()).Fields() {
|
for _, f := range newDB.NewScope(field.Field.Addr().Interface()).Fields() {
|
||||||
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Field.Interface()))
|
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Field.Interface()))
|
||||||
}
|
}
|
||||||
scope.SetColumn(field.Name, value.Interface())
|
scope.SetColumn(field.Name, value.Interface())
|
||||||
|
@ -33,7 +33,7 @@ func SaveBeforeAssociations(scope *Scope) {
|
||||||
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
||||||
|
|
||||||
if relationship.ForeignKey != "" {
|
if relationship.ForeignKey != "" {
|
||||||
scope.SetColumn(relationship.ForeignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
scope.SetColumn(relationship.ForeignKey, newDB.NewScope(value.Addr().Interface()).PrimaryKeyValue())
|
||||||
}
|
}
|
||||||
if relationship.ForeignType != "" {
|
if relationship.ForeignType != "" {
|
||||||
scope.Err(fmt.Errorf("gorm does not support polymorphic belongs_to associations"))
|
scope.Err(fmt.Errorf("gorm does not support polymorphic belongs_to associations"))
|
||||||
|
@ -107,7 +107,7 @@ func SaveAfterAssociations(scope *Scope) {
|
||||||
} else {
|
} else {
|
||||||
destValue := reflect.New(field.Field.Type()).Elem()
|
destValue := reflect.New(field.Field.Type()).Elem()
|
||||||
|
|
||||||
for _, f := range newDB.NewScope(field.Field.Interface()).Fields() {
|
for _, f := range newDB.NewScope(field.Field.Addr().Interface()).Fields() {
|
||||||
destValue.FieldByName(f.Name).Set(f.Field)
|
destValue.FieldByName(f.Name).Set(f.Field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue