mirror of https://github.com/go-gorm/gorm.git
Fix join table foreign key in snake_case
This commit is contained in:
parent
2416eabd3f
commit
d4f8a52442
|
@ -210,7 +210,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||||
for idx, ownField := range ownForeignFields {
|
for idx, ownField := range ownForeignFields {
|
||||||
joinFieldName := schema.Name + ownField.Name
|
joinFieldName := schema.Name + ownField.Name
|
||||||
if len(joinForeignKeys) > idx {
|
if len(joinForeignKeys) > idx {
|
||||||
joinFieldName = joinForeignKeys[idx]
|
joinFieldName = strings.Title(joinForeignKeys[idx])
|
||||||
}
|
}
|
||||||
|
|
||||||
ownFieldsMap[joinFieldName] = true
|
ownFieldsMap[joinFieldName] = true
|
||||||
|
@ -226,7 +226,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||||
for idx, relField := range refForeignFields {
|
for idx, relField := range refForeignFields {
|
||||||
joinFieldName := relation.FieldSchema.Name + relField.Name
|
joinFieldName := relation.FieldSchema.Name + relField.Name
|
||||||
if len(joinReferences) > idx {
|
if len(joinReferences) > idx {
|
||||||
joinFieldName = joinReferences[idx]
|
joinFieldName = strings.Title(joinReferences[idx])
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := ownFieldsMap[joinFieldName]; ok {
|
if _, ok := ownFieldsMap[joinFieldName]; ok {
|
||||||
|
|
|
@ -139,6 +139,7 @@ func TestMany2ManyOverrideForeignKeyAndReferences(t *testing.T) {
|
||||||
type User struct {
|
type User struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Profiles []Profile `gorm:"many2many:user_profiles;ForeignKey:Refer;JoinForeignKey:UserReferID;References:UserRefer;JoinReferences:ProfileRefer"`
|
Profiles []Profile `gorm:"many2many:user_profiles;ForeignKey:Refer;JoinForeignKey:UserReferID;References:UserRefer;JoinReferences:ProfileRefer"`
|
||||||
|
Profiles2 []Profile `gorm:"many2many:user_profiles2;ForeignKey:refer;JoinForeignKey:user_refer_id;References:user_refer;JoinReferences:profile_refer"`
|
||||||
Refer uint
|
Refer uint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,6 +150,13 @@ func TestMany2ManyOverrideForeignKeyAndReferences(t *testing.T) {
|
||||||
{"Refer", "User", "UserReferID", "user_profiles", "", true},
|
{"Refer", "User", "UserReferID", "user_profiles", "", true},
|
||||||
{"UserRefer", "Profile", "ProfileRefer", "user_profiles", "", false},
|
{"UserRefer", "Profile", "ProfileRefer", "user_profiles", "", false},
|
||||||
},
|
},
|
||||||
|
}, Relation{
|
||||||
|
Name: "Profiles2", Type: schema.Many2Many, Schema: "User", FieldSchema: "Profile",
|
||||||
|
JoinTable: JoinTable{Name: "user_profiles2", Table: "user_profiles2"},
|
||||||
|
References: []Reference{
|
||||||
|
{"Refer", "User", "User_refer_id", "user_profiles2", "", true},
|
||||||
|
{"UserRefer", "Profile", "Profile_refer", "user_profiles2", "", false},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue