From 6e38a2c2d510a6823ad7b73c7e9321c8f7ceaff8 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 6 Sep 2020 10:51:21 +0800 Subject: [PATCH] Fix many2many join table name rule --- schema/naming.go | 4 ++++ schema/relationship_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/schema/naming.go b/schema/naming.go index 9b7c9471..ecdab791 100644 --- a/schema/naming.go +++ b/schema/naming.go @@ -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) } diff --git a/schema/relationship_test.go b/schema/relationship_test.go index f2d63323..b9279b9f 100644 --- a/schema/relationship_test.go +++ b/schema/relationship_test.go @@ -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}, }, }) }