Skip to content

Commit

Permalink
Don't check relations if field is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Aug 14, 2014
1 parent 131b504 commit febc826
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
90 changes: 46 additions & 44 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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 = ""
}
}
if !field.IsIgnored {
// parse association
typ := indirectValue.Type()
foreignKey := SnakeToUpperCamel(settings["FOREIGNKEY"])
associationForeignKey := SnakeToUpperCamel(settings["ASSOCIATIONFOREIGNKEY"])
many2many := settings["MANY2MANY"]
scopeTyp := scope.IndirectValue().Type()

field.Relationship = &relationship{
JoinTable: many2many,
ForeignKey: foreignKey,
AssociationForeignKey: associationForeignKey,
Kind: "has_many",
}
switch indirectValue.Kind() {
case reflect.Slice:
typ = typ.Elem()

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"}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ type Product struct {
}

type Company struct {
Id int64
Name string
Id int64
Name string
Owner *User `sql:"-"`
}

type Role struct {
Expand Down

0 comments on commit febc826

Please sign in to comment.