diff --git a/association.go b/association.go index 48b051f8..39396e16 100644 --- a/association.go +++ b/association.go @@ -126,7 +126,8 @@ func (association *Association) Replace(values ...interface{}) *Association { if relationship.Kind == "many_to_many" { association.setErr(relationship.JoinTableHandler.Delete(relationship.JoinTableHandler, query, relationship)) } else if relationship.Kind == "has_one" || relationship.Kind == "has_many" { - query.Update(foreignKeyMap) + fieldValue := reflect.New(association.Field.Field.Type()).Interface() + association.setErr(query.Model(fieldValue).UpdateColumn(foreignKeyMap).Error) } return association } diff --git a/association_test.go b/association_test.go index 6cd76634..b2d091c2 100644 --- a/association_test.go +++ b/association_test.go @@ -175,11 +175,18 @@ func TestHasMany(t *testing.T) { var comments3 []Comment DB.Model(&post).Related(&comments3) if !compareComments(comments3, []string{"Comment 3"}) { - fmt.Println(comments3) t.Errorf("Delete an existing resource for has many relations") } // Replace + DB.Model(&post).Association("Comments").Replace(&Comment{Content: "Comment 4"}, &Comment{Content: "Comment 5"}) + + var comments4 []Comment + DB.Model(&post).Related(&comments4) + if !compareComments(comments4, []string{"Comment 4", "Comment 5"}) { + t.Errorf("Replace has many relations") + } + // Clear }