Fix many2many join table name rule

This commit is contained in:
Jinzhu 2020-09-06 10:51:21 +08:00
parent d8ddccf147
commit 6e38a2c2d5
2 changed files with 8 additions and 4 deletions

View File

@ -41,6 +41,10 @@ func (ns NamingStrategy) ColumnName(table, column string) string {
// JoinTableName convert string to join table name
func (ns NamingStrategy) JoinTableName(str string) string {
if strings.ToLower(str) == str {
return str
}
if ns.SingularTable {
return ns.TablePrefix + toDBName(str)
}

View File

@ -206,16 +206,16 @@ func TestMany2ManyOverrideJoinForeignKey(t *testing.T) {
type User struct {
gorm.Model
Profiles []Profile `gorm:"many2many:user_profiles;JoinForeignKey:UserReferID;JoinReferences:ProfileRefer"`
Profiles []Profile `gorm:"many2many:user_profile;JoinForeignKey:UserReferID;JoinReferences:ProfileRefer"`
Refer uint
}
checkStructRelation(t, &User{}, Relation{
Name: "Profiles", Type: schema.Many2Many, Schema: "User", FieldSchema: "Profile",
JoinTable: JoinTable{Name: "user_profiles", Table: "user_profiles"},
JoinTable: JoinTable{Name: "user_profile", Table: "user_profile"},
References: []Reference{
{"ID", "User", "UserReferID", "user_profiles", "", true},
{"ID", "Profile", "ProfileRefer", "user_profiles", "", false},
{"ID", "User", "UserReferID", "user_profile", "", true},
{"ID", "Profile", "ProfileRefer", "user_profile", "", false},
},
})
}