mirror of https://github.com/go-gorm/gorm.git
Fix stack overflow for many to many associations
This commit is contained in:
parent
4e8d43dd4f
commit
b391029188
|
@ -195,7 +195,12 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
|
||||
switch indirectType.Kind() {
|
||||
case reflect.Slice:
|
||||
if toStructFields := toScope.GetStructFields(); len(toStructFields) > 0 {
|
||||
elemType := indirectType.Elem()
|
||||
if elemType.Kind() == reflect.Ptr {
|
||||
elemType = elemType.Elem()
|
||||
}
|
||||
|
||||
if elemType.Kind() == reflect.Struct {
|
||||
if foreignKey == "" {
|
||||
foreignKey = scopeType.Name() + "Id"
|
||||
}
|
||||
|
@ -206,7 +211,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
|
||||
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
||||
if associationForeignKey == "" {
|
||||
associationForeignKey = toScope.GetModelStruct().ModelType.Name() + "Id"
|
||||
associationForeignKey = elemType.Name() + "Id"
|
||||
}
|
||||
|
||||
relationship.ForeignFieldName = foreignKey
|
||||
|
@ -216,7 +221,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
field.Relationship = relationship
|
||||
} else {
|
||||
relationship.Kind = "has_many"
|
||||
if foreignField := getForeignField(foreignKey, toStructFields); foreignField != nil {
|
||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||
relationship.ForeignFieldName = foreignField.Name
|
||||
relationship.ForeignDBName = foreignField.DBName
|
||||
foreignField.IsForeignKey = true
|
||||
|
|
|
@ -63,8 +63,9 @@ type Address struct {
|
|||
}
|
||||
|
||||
type Language struct {
|
||||
Id int
|
||||
Name string
|
||||
Id int
|
||||
Name string
|
||||
Users []User `gorm:"many2many:user_languages;"`
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
|
|
Loading…
Reference in New Issue