forked from mirror/gorm
Fix test TestHasOneAndHasManyAssociation
This commit is contained in:
parent
4e8272cf9d
commit
7decf73356
|
@ -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
|
||||
|
|
|
@ -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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue