From 14e0507fd2d31c10406811fe10f2c024e98d0b93 Mon Sep 17 00:00:00 2001 From: Wendell Sun Date: Thu, 14 Mar 2019 12:12:38 +0800 Subject: [PATCH] fix the table name of many2many --- customize_column_test.go | 11 +++++++++++ model_struct.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/customize_column_test.go b/customize_column_test.go index 5e19d6f4..c236ac24 100644 --- a/customize_column_test.go +++ b/customize_column_test.go @@ -289,6 +289,9 @@ type SelfReferencingUser struct { func TestSelfReferencingMany2ManyColumn(t *testing.T) { DB.DropTable(&SelfReferencingUser{}, "UserFriends") DB.AutoMigrate(&SelfReferencingUser{}) + if !DB.HasTable("UserFriends") { + t.Errorf("auto migrate error, table UserFriends should be created") + } friend1 := SelfReferencingUser{Name: "friend1_m2m"} if err := DB.Create(&friend1).Error; err != nil { @@ -313,6 +316,14 @@ func TestSelfReferencingMany2ManyColumn(t *testing.T) { t.Errorf("Should find created friends correctly") } + var count int + if err := DB.Table("UserFriends").Count(&count).Error; err != nil { + t.Errorf("no error should happen, but got %v", err) + } + if count == 0 { + t.Errorf("table UserFriends should have records") + } + var newUser = SelfReferencingUser{} if err := DB.Preload("Friends").First(&newUser, "id = ?", user.ID).Error; err != nil { diff --git a/model_struct.go b/model_struct.go index 9e93db63..a1e6c0e2 100644 --- a/model_struct.go +++ b/model_struct.go @@ -340,7 +340,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct { } joinTableHandler := JoinTableHandler{} - joinTableHandler.Setup(relationship, ToTableName(many2many), reflectType, elemType) + joinTableHandler.Setup(relationship, many2many, reflectType, elemType) relationship.JoinTableHandler = &joinTableHandler field.Relationship = relationship } else {