From a952601f21dbd6b0b4fd574d8c48c5bb1b26e58e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 3 Apr 2015 16:32:53 +0800 Subject: [PATCH] Fix query related resources with customized column name --- scope_private.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scope_private.go b/scope_private.go index 8de12ced..99dda2ed 100644 --- a/scope_private.go +++ b/scope_private.go @@ -395,8 +395,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { fromFields := scope.Fields() toFields := toScope.Fields() for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") { - fromField := fromFields[ToDBName(foreignKey)] - toField := toFields[ToDBName(foreignKey)] + var fromField, toField *Field + 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 relationship := fromField.Relationship; relationship != nil {