Set Error to Association when some error happend

This commit is contained in:
Jinzhu 2016-01-03 17:52:16 +08:00
parent 2e949b02ae
commit b59eaf526e
2 changed files with 11 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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)
}