Fix test TestRelated

This commit is contained in:
Jinzhu 2015-07-30 18:56:05 +08:00
parent 7decf73356
commit 9c52c29e90
3 changed files with 6 additions and 7 deletions

View File

@ -74,7 +74,7 @@ func SaveAfterAssociations(scope *Scope) {
if 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 {
if f, ok := scope.FieldByName(associationForeignName); ok {
scope.Err(newScope.SetColumn(fieldName, f.Field.Interface()))
}
}

View File

@ -325,10 +325,9 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
relationship.Kind = "belongs_to"
field.Relationship = relationship
} else {
hasOneForeignKeys := foreignKeys
if len(hasOneForeignKeys) == 0 {
if len(foreignKeys) == 0 {
for _, field := range toScope.PrimaryFields() {
if foreignField := getForeignField(modelStruct.ModelType.Name()+field.Name, fields); foreignField != nil {
if foreignField := getForeignField(modelStruct.ModelType.Name()+field.Name, toScope.GetStructFields()); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
@ -337,7 +336,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
}
}
} else {
for _, foreignKey := range hasOneForeignKeys {
for _, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scope.PrimaryField().Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scope.PrimaryField().DBName)

View File

@ -417,8 +417,8 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
} else if relationship.Kind == "belongs_to" {
query := toScope.db
for idx, foreignKey := range relationship.ForeignDBNames {
if field, ok := scope.FieldByName(relationship.AssociationForeignDBNames[idx]); ok {
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
if field, ok := scope.FieldByName(foreignKey); ok {
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.AssociationForeignDBNames[idx])), field.Field.Interface())
}
}
scope.Err(query.Find(value).Error)