diff --git a/callback_shared.go b/callback_shared.go index fc6b23b3..547059e3 100644 --- a/callback_shared.go +++ b/callback_shared.go @@ -74,7 +74,7 @@ func SaveAfterAssociations(scope *Scope) { if len(relationship.ForeignFieldNames) != 0 { for idx, fieldName := range relationship.ForeignFieldNames { associationForeignName := relationship.AssociationForeignDBNames[idx] - if f, ok := scope.New(value.Addr().Interface()).FieldByName(associationForeignName); ok { + if f, ok := scope.FieldByName(associationForeignName); ok { scope.Err(newScope.SetColumn(fieldName, f.Field.Interface())) } } diff --git a/model_struct.go b/model_struct.go index 902bc2cd..aa124625 100644 --- a/model_struct.go +++ b/model_struct.go @@ -325,10 +325,9 @@ func (scope *Scope) GetModelStruct() *ModelStruct { relationship.Kind = "belongs_to" field.Relationship = relationship } else { - hasOneForeignKeys := foreignKeys - if len(hasOneForeignKeys) == 0 { + if len(foreignKeys) == 0 { for _, field := range toScope.PrimaryFields() { - if foreignField := getForeignField(modelStruct.ModelType.Name()+field.Name, fields); foreignField != nil { + if foreignField := getForeignField(modelStruct.ModelType.Name()+field.Name, toScope.GetStructFields()); foreignField != nil { relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name) relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName) relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name) @@ -337,7 +336,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct { } } } else { - for _, foreignKey := range hasOneForeignKeys { + for _, foreignKey := range foreignKeys { if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil { relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name) relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName) diff --git a/scope_private.go b/scope_private.go index 931db3de..1d58e6a2 100644 --- a/scope_private.go +++ b/scope_private.go @@ -417,8 +417,8 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { } else if relationship.Kind == "belongs_to" { query := toScope.db for idx, foreignKey := range relationship.ForeignDBNames { - if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok { - query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface()) + if field, ok := scope.FieldByName(foreignKey); ok { + query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.AssociationForeignDBNames[idx])), field.Field.Interface()) } } scope.Err(query.Find(value).Error)