Fix query related resources with customized column name

This commit is contained in:
Jinzhu 2015-04-03 16:32:53 +08:00
parent 03966a1e20
commit a952601f21
1 changed files with 11 additions and 2 deletions

View File

@ -395,8 +395,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
fromFields := scope.Fields() fromFields := scope.Fields()
toFields := toScope.Fields() toFields := toScope.Fields()
for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") { for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
fromField := fromFields[ToDBName(foreignKey)] var fromField, toField *Field
toField := toFields[ToDBName(foreignKey)] if field, ok := scope.FieldByName(foreignKey); ok {
fromField = field
} else {
fromField = fromFields[ToDBName(foreignKey)]
}
if field, ok := toScope.FieldByName(foreignKey); ok {
toField = field
} else {
toField = toFields[ToDBName(foreignKey)]
}
if fromField != nil { if fromField != nil {
if relationship := fromField.Relationship; relationship != nil { if relationship := fromField.Relationship; relationship != nil {