From febc82651175e4c9d077497a9f7f7e8e4b44f2b1 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 14 Aug 2014 21:45:42 +0800 Subject: [PATCH] Don't check relations if field is ignored --- scope.go | 90 +++++++++++++++++++++++++----------------------- scope_private.go | 1 + structs_test.go | 5 +-- 3 files changed, 50 insertions(+), 46 deletions(-) diff --git a/scope.go b/scope.go index ae203d1d..79952375 100644 --- a/scope.go +++ b/scope.go @@ -265,55 +265,57 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field { field.Tag = fieldStruct.Tag field.SqlTag = scope.sqlTagForField(&field) - // parse association - typ := indirectValue.Type() - foreignKey := SnakeToUpperCamel(settings["FOREIGNKEY"]) - associationForeignKey := SnakeToUpperCamel(settings["ASSOCIATIONFOREIGNKEY"]) - many2many := settings["MANY2MANY"] - scopeTyp := scope.IndirectValue().Type() + if !field.IsIgnored { + // parse association + typ := indirectValue.Type() + foreignKey := SnakeToUpperCamel(settings["FOREIGNKEY"]) + associationForeignKey := SnakeToUpperCamel(settings["ASSOCIATIONFOREIGNKEY"]) + many2many := settings["MANY2MANY"] + scopeTyp := scope.IndirectValue().Type() - switch indirectValue.Kind() { - case reflect.Slice: - typ = typ.Elem() + switch indirectValue.Kind() { + case reflect.Slice: + typ = typ.Elem() - if typ.Kind() == reflect.Struct { - if foreignKey == "" { - foreignKey = scopeTyp.Name() + "Id" - } - if associationForeignKey == "" { - associationForeignKey = typ.Name() + "Id" - } - - // if not many to many, foreign key could be null - if many2many == "" { - if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() { - foreignKey = "" - } - } - - field.Relationship = &relationship{ - JoinTable: many2many, - ForeignKey: foreignKey, - AssociationForeignKey: associationForeignKey, - Kind: "has_many", - } - - if many2many != "" { - field.Relationship.Kind = "many_to_many" - } - } - case reflect.Struct: - if !field.IsTime() && !field.IsScanner() { - if foreignKey == "" && scope.HasColumn(field.Name+"Id") { - field.Relationship = &relationship{ForeignKey: field.Name + "Id", Kind: "belongs_to"} - } else if scope.HasColumn(foreignKey) { - field.Relationship = &relationship{ForeignKey: foreignKey, Kind: "belongs_to"} - } else { + if typ.Kind() == reflect.Struct { if foreignKey == "" { foreignKey = scopeTyp.Name() + "Id" } - if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() { - field.Relationship = &relationship{ForeignKey: foreignKey, Kind: "has_one"} + if associationForeignKey == "" { + associationForeignKey = typ.Name() + "Id" + } + + // if not many to many, foreign key could be null + if many2many == "" { + if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() { + foreignKey = "" + } + } + + field.Relationship = &relationship{ + JoinTable: many2many, + ForeignKey: foreignKey, + AssociationForeignKey: associationForeignKey, + Kind: "has_many", + } + + if many2many != "" { + field.Relationship.Kind = "many_to_many" + } + } + case reflect.Struct: + if !field.IsTime() && !field.IsScanner() { + if foreignKey == "" && scope.HasColumn(field.Name+"Id") { + field.Relationship = &relationship{ForeignKey: field.Name + "Id", Kind: "belongs_to"} + } else if scope.HasColumn(foreignKey) { + field.Relationship = &relationship{ForeignKey: foreignKey, Kind: "belongs_to"} + } else { + if foreignKey == "" { + foreignKey = scopeTyp.Name() + "Id" + } + if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() { + field.Relationship = &relationship{ForeignKey: foreignKey, Kind: "has_one"} + } } } } diff --git a/scope_private.go b/scope_private.go index 4f25532a..493742da 100644 --- a/scope_private.go +++ b/scope_private.go @@ -308,6 +308,7 @@ func (scope *Scope) sqlTagForField(field *Field) (typ string) { fieldTag := field.Tag.Get(scope.db.parent.tagIdentifier) if fieldTag == "-" { field.IsIgnored = true + return } var setting = parseTagSetting(fieldTag) diff --git a/structs_test.go b/structs_test.go index e6aff458..02df18eb 100644 --- a/structs_test.go +++ b/structs_test.go @@ -84,8 +84,9 @@ type Product struct { } type Company struct { - Id int64 - Name string + Id int64 + Name string + Owner *User `sql:"-"` } type Role struct {