From 7decf73356c289d71ed2bf74bbc854777eede3a5 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 30 Jul 2015 18:41:43 +0800 Subject: [PATCH] Fix test TestHasOneAndHasManyAssociation --- association_test.go | 6 +++--- callback_shared.go | 6 +++--- model_struct.go | 11 +++++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/association_test.go b/association_test.go index ea5b1b80..205a929e 100644 --- a/association_test.go +++ b/association_test.go @@ -23,11 +23,11 @@ func TestHasOneAndHasManyAssociation(t *testing.T) { } if err := DB.Save(&post).Error; err != nil { - t.Errorf("Got errors when save post") + t.Errorf("Got errors when save post", err.Error()) } - if DB.First(&Category{}, "name = ?", "Category 1").Error != nil { - t.Errorf("Category should be saved") + if err := DB.First(&Category{}, "name = ?", "Category 1").Error; err != nil { + t.Errorf("Category should be saved", err.Error()) } var p Post diff --git a/callback_shared.go b/callback_shared.go index 1e9d320f..fc6b23b3 100644 --- a/callback_shared.go +++ b/callback_shared.go @@ -52,8 +52,8 @@ func SaveAfterAssociations(scope *Scope) { if relationship.JoinTableHandler == nil && 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 { - scope.Err(scope.SetColumn(fieldName, f.Field.Interface())) + if f, ok := scope.FieldByName(associationForeignName); ok { + scope.Err(newScope.SetColumn(fieldName, f.Field.Interface())) } } } @@ -75,7 +75,7 @@ func SaveAfterAssociations(scope *Scope) { for idx, fieldName := range relationship.ForeignFieldNames { associationForeignName := relationship.AssociationForeignDBNames[idx] if f, ok := scope.New(value.Addr().Interface()).FieldByName(associationForeignName); ok { - scope.Err(scope.SetColumn(fieldName, f.Field.Interface())) + scope.Err(newScope.SetColumn(fieldName, f.Field.Interface())) } } } diff --git a/model_struct.go b/model_struct.go index 7e4b683c..902bc2cd 100644 --- a/model_struct.go +++ b/model_struct.go @@ -299,12 +299,11 @@ func (scope *Scope) GetModelStruct() *ModelStruct { } continue } else { - belongsToForeignKeys := foreignKeys - if len(belongsToForeignKeys) == 0 { - for _, field := range toScope.PrimaryFields() { - if foreignField := getForeignField(field.Name+field.Name, fields); foreignField != nil { - relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name) - relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName) + if len(foreignKeys) == 0 { + for _, f := range toScope.PrimaryFields() { + if foreignField := getForeignField(field.Name+f.Name, fields); foreignField != nil { + relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name) + relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName) relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name) relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName) foreignField.IsForeignKey = true