diff --git a/association.go b/association.go index 1ba1d519..a382abf2 100644 --- a/association.go +++ b/association.go @@ -42,7 +42,7 @@ func (association *Association) saveAssociations(values ...interface{}) *Associa // value has to been saved for many2many if relationship.Kind == "many_to_many" { if scope.New(reflectValue.Interface()).PrimaryKeyZero() { - scope.NewDB().Save(reflectValue.Interface()) + association.setErr(scope.NewDB().Save(reflectValue.Interface()).Error) } } diff --git a/migration_test.go b/migration_test.go index 57d9530a..0411872e 100644 --- a/migration_test.go +++ b/migration_test.go @@ -71,6 +71,16 @@ func TestIndexes(t *testing.T) { t.Errorf("Should get to create duplicate record when having unique index") } + var user = User{Name: "sample_user"} + DB.Save(&user) + if DB.Model(&user).Association("Emails").Append(Email{Email: "not-1duplicated@gmail.com"}, Email{Email: "not-duplicated2@gmail.com"}).Error != nil { + t.Errorf("Should get no error when append two emails for user") + } + + if DB.Model(&user).Association("Emails").Append(Email{Email: "duplicated@gmail.com"}, Email{Email: "duplicated@gmail.com"}).Error == nil { + t.Errorf("Should get no duplicated email error when insert duplicated emails for a user") + } + if err := DB.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil { t.Errorf("Got error when tried to remove index: %+v", err) }