Skip to content

Commit

Permalink
Fix test TestHasOneAndHasManyAssociation
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 30, 2015
1 parent 4e8272c commit 7decf73
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
6 changes: 3 additions & 3 deletions association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions callback_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
}
Expand All @@ -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()))
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7decf73

Please sign in to comment.