From b59eaf526ef0e1aac850c2e95ee48fa77afb1498 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 3 Jan 2016 17:52:16 +0800 Subject: [PATCH] Set Error to Association when some error happend --- association.go | 2 +- migration_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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) }