Skip to content

Commit

Permalink
Fix set ForeignKey for embedded struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Dec 4, 2014
1 parent 6d13ae4 commit cbcb88d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions callback_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func SaveBeforeAssociations(scope *Scope) {
if !value.CanAddr() {
// If can't take address, then clone the value and set it back
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()))
}
scope.SetColumn(field.Name, value.Interface())
}
scope.Err(newDB.Save(value.Addr().Interface()).Error)

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 != "" {
scope.Err(fmt.Errorf("gorm does not support polymorphic belongs_to associations"))
Expand Down Expand Up @@ -107,7 +107,7 @@ func SaveAfterAssociations(scope *Scope) {
} else {
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)
}

Expand Down

0 comments on commit cbcb88d

Please sign in to comment.