forked from mirror/gorm
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
|
||||
}
|
||||
|
||||
type JoinTableSource struct {
|
||||
ModelType reflect.Type
|
||||
ForeignKeys []struct {
|
||||
type JoinTableForeignKey struct {
|
||||
DBName 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 {
|
||||
|
|
|
@ -146,6 +146,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
}
|
||||
}
|
||||
|
||||
defer func() {
|
||||
for _, field := range fields {
|
||||
if !field.IsIgnored {
|
||||
fieldStruct := field.Struct
|
||||
|
@ -205,8 +206,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
|
||||
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
|
||||
relationship.Kind = "many_to_many"
|
||||
relationship.JoinTableHandler = JoinTableHandler{}
|
||||
|
||||
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
||||
if associationForeignKey == "" {
|
||||
associationForeignKey = elemType.Name() + "Id"
|
||||
|
@ -216,6 +215,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
relationship.ForeignDBName = ToDBName(foreignKey)
|
||||
relationship.AssociationForeignFieldName = associationForeignKey
|
||||
relationship.AssociationForeignDBName = ToDBName(associationForeignKey)
|
||||
relationship.JoinTableHandler = &JoinTableHandler{
|
||||
TableName: many2many,
|
||||
Source: JoinTableSource{ModelType: scopeType},
|
||||
Destination: JoinTableSource{ModelType: elemType},
|
||||
}
|
||||
updateJoinTableHandler(relationship)
|
||||
|
||||
field.Relationship = relationship
|
||||
} else {
|
||||
relationship.Kind = "has_many"
|
||||
|
@ -283,6 +289,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
}
|
||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||
}
|
||||
}()
|
||||
|
||||
modelStructs[scopeType] = &modelStruct
|
||||
|
||||
|
|
Loading…
Reference in New Issue