fixed has_many stopped working if field names are identical (#4387)
* fixed belongs_to & has_one reversed if field same * hasmany same foreign key bug fixed and test added
This commit is contained in:
parent
cf93b16730
commit
79f427d862
@ -408,7 +408,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
|
|||||||
ff := foreignSchema.LookUpField(foreignKey)
|
ff := foreignSchema.LookUpField(foreignKey)
|
||||||
pf := primarySchema.LookUpField(foreignKey)
|
pf := primarySchema.LookUpField(foreignKey)
|
||||||
isKeySame := utils.ExistsIn(foreignKey, &relation.primaryKeys)
|
isKeySame := utils.ExistsIn(foreignKey, &relation.primaryKeys)
|
||||||
if ff == nil || (pf != nil && ff != nil && schema == primarySchema && primarySchema != foreignSchema && !isKeySame) {
|
if ff == nil || (pf != nil && ff != nil && schema == primarySchema && primarySchema != foreignSchema && !isKeySame && field.IndirectFieldType.Kind() == reflect.Struct) {
|
||||||
reguessOrErr()
|
reguessOrErr()
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
@ -501,3 +501,22 @@ func TestBelongsToWithSameForeignKey(t *testing.T) {
|
|||||||
References: []Reference{{"ID", "Profile", "ProfileRefer", "User", "", false}},
|
References: []Reference{{"ID", "Profile", "ProfileRefer", "User", "", false}},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHasManySameForeignKey(t *testing.T) {
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
UserRefer uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
UserRefer uint
|
||||||
|
Profile []Profile `gorm:"ForeignKey:UserRefer"`
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStructRelation(t, &User{}, Relation{
|
||||||
|
Name: "Profile", Type: schema.HasMany, Schema: "User", FieldSchema: "Profile",
|
||||||
|
References: []Reference{{"ID", "User", "UserRefer", "Profile", "", true}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user