mirror of https://github.com/go-gorm/gorm.git
Fix stack overflow
This commit is contained in:
parent
6ba0c1661f
commit
fa753969b1
|
@ -13,12 +13,36 @@ type JoinTableHandlerInterface interface {
|
||||||
JoinWith(db *DB, source interface{}) *DB
|
JoinWith(db *DB, source interface{}) *DB
|
||||||
}
|
}
|
||||||
|
|
||||||
type JoinTableSource struct {
|
type JoinTableForeignKey struct {
|
||||||
ModelType reflect.Type
|
|
||||||
ForeignKeys []struct {
|
|
||||||
DBName string
|
DBName string
|
||||||
AssociationDBName string
|
AssociationDBName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateJoinTableHandler(relationship *Relationship) {
|
||||||
|
handler := relationship.JoinTableHandler.(*JoinTableHandler)
|
||||||
|
|
||||||
|
destinationScope := &Scope{Value: reflect.New(handler.Destination.ModelType).Interface()}
|
||||||
|
for _, primaryField := range destinationScope.GetModelStruct().PrimaryFields {
|
||||||
|
db := relationship.ForeignDBName
|
||||||
|
handler.Destination.ForeignKeys = append(handler.Destination.ForeignKeys, JoinTableForeignKey{
|
||||||
|
DBName: db,
|
||||||
|
AssociationDBName: primaryField.DBName,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sourceScope := &Scope{Value: reflect.New(handler.Source.ModelType).Interface()}
|
||||||
|
for _, primaryField := range sourceScope.GetModelStruct().PrimaryFields {
|
||||||
|
db := relationship.AssociationForeignDBName
|
||||||
|
handler.Source.ForeignKeys = append(handler.Source.ForeignKeys, JoinTableForeignKey{
|
||||||
|
DBName: db,
|
||||||
|
AssociationDBName: primaryField.DBName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type JoinTableSource struct {
|
||||||
|
ModelType reflect.Type
|
||||||
|
ForeignKeys []JoinTableForeignKey
|
||||||
}
|
}
|
||||||
|
|
||||||
type JoinTableHandler struct {
|
type JoinTableHandler struct {
|
||||||
|
|
|
@ -146,6 +146,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
if !field.IsIgnored {
|
if !field.IsIgnored {
|
||||||
fieldStruct := field.Struct
|
fieldStruct := field.Struct
|
||||||
|
@ -205,8 +206,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
|
|
||||||
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
|
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
|
||||||
relationship.Kind = "many_to_many"
|
relationship.Kind = "many_to_many"
|
||||||
relationship.JoinTableHandler = JoinTableHandler{}
|
|
||||||
|
|
||||||
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
||||||
if associationForeignKey == "" {
|
if associationForeignKey == "" {
|
||||||
associationForeignKey = elemType.Name() + "Id"
|
associationForeignKey = elemType.Name() + "Id"
|
||||||
|
@ -216,6 +215,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
relationship.ForeignDBName = ToDBName(foreignKey)
|
relationship.ForeignDBName = ToDBName(foreignKey)
|
||||||
relationship.AssociationForeignFieldName = associationForeignKey
|
relationship.AssociationForeignFieldName = associationForeignKey
|
||||||
relationship.AssociationForeignDBName = ToDBName(associationForeignKey)
|
relationship.AssociationForeignDBName = ToDBName(associationForeignKey)
|
||||||
|
relationship.JoinTableHandler = &JoinTableHandler{
|
||||||
|
TableName: many2many,
|
||||||
|
Source: JoinTableSource{ModelType: scopeType},
|
||||||
|
Destination: JoinTableSource{ModelType: elemType},
|
||||||
|
}
|
||||||
|
updateJoinTableHandler(relationship)
|
||||||
|
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
} else {
|
} else {
|
||||||
relationship.Kind = "has_many"
|
relationship.Kind = "has_many"
|
||||||
|
@ -283,6 +289,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
modelStructs[scopeType] = &modelStruct
|
modelStructs[scopeType] = &modelStruct
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue