Fix test TestHasOneAndHasManyAssociation

This commit is contained in:
Jinzhu 2015-07-30 18:41:43 +08:00
parent 4e8272cf9d
commit 7decf73356
3 changed files with 11 additions and 12 deletions

View File

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

View File

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

View File

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