forked from mirror/gorm
Don't check relations if field is ignored
This commit is contained in:
parent
131b504941
commit
febc826511
90
scope.go
90
scope.go
|
@ -265,55 +265,57 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
|
||||||
field.Tag = fieldStruct.Tag
|
field.Tag = fieldStruct.Tag
|
||||||
field.SqlTag = scope.sqlTagForField(&field)
|
field.SqlTag = scope.sqlTagForField(&field)
|
||||||
|
|
||||||
// parse association
|
if !field.IsIgnored {
|
||||||
typ := indirectValue.Type()
|
// parse association
|
||||||
foreignKey := SnakeToUpperCamel(settings["FOREIGNKEY"])
|
typ := indirectValue.Type()
|
||||||
associationForeignKey := SnakeToUpperCamel(settings["ASSOCIATIONFOREIGNKEY"])
|
foreignKey := SnakeToUpperCamel(settings["FOREIGNKEY"])
|
||||||
many2many := settings["MANY2MANY"]
|
associationForeignKey := SnakeToUpperCamel(settings["ASSOCIATIONFOREIGNKEY"])
|
||||||
scopeTyp := scope.IndirectValue().Type()
|
many2many := settings["MANY2MANY"]
|
||||||
|
scopeTyp := scope.IndirectValue().Type()
|
||||||
|
|
||||||
switch indirectValue.Kind() {
|
switch indirectValue.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
typ = typ.Elem()
|
typ = typ.Elem()
|
||||||
|
|
||||||
if typ.Kind() == reflect.Struct {
|
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 foreignKey == "" {
|
if foreignKey == "" {
|
||||||
foreignKey = scopeTyp.Name() + "Id"
|
foreignKey = scopeTyp.Name() + "Id"
|
||||||
}
|
}
|
||||||
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
if associationForeignKey == "" {
|
||||||
field.Relationship = &relationship{ForeignKey: foreignKey, Kind: "has_one"}
|
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"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -308,6 +308,7 @@ func (scope *Scope) sqlTagForField(field *Field) (typ string) {
|
||||||
fieldTag := field.Tag.Get(scope.db.parent.tagIdentifier)
|
fieldTag := field.Tag.Get(scope.db.parent.tagIdentifier)
|
||||||
if fieldTag == "-" {
|
if fieldTag == "-" {
|
||||||
field.IsIgnored = true
|
field.IsIgnored = true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var setting = parseTagSetting(fieldTag)
|
var setting = parseTagSetting(fieldTag)
|
||||||
|
|
|
@ -84,8 +84,9 @@ type Product struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Company struct {
|
type Company struct {
|
||||||
Id int64
|
Id int64
|
||||||
Name string
|
Name string
|
||||||
|
Owner *User `sql:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Role struct {
|
type Role struct {
|
||||||
|
|
Loading…
Reference in New Issue