From d22215129ee4747f9a9dd5b089d9f6920efc91ad Mon Sep 17 00:00:00 2001 From: li-jin-gou <97824201+li-jin-gou@users.noreply.github.com> Date: Tue, 8 Feb 2022 17:06:10 +0800 Subject: [PATCH] fix: replace empty table name result in panic (#5048) * fix: replace empty name result in panic * fix: replace empty table name result in panic --- schema/naming.go | 8 +++++++- schema/naming_test.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/schema/naming.go b/schema/naming.go index 8407bffa..a4e3a75b 100644 --- a/schema/naming.go +++ b/schema/naming.go @@ -120,7 +120,13 @@ func (ns NamingStrategy) toDBName(name string) string { } if ns.NameReplacer != nil { - name = ns.NameReplacer.Replace(name) + tmpName := ns.NameReplacer.Replace(name) + + if tmpName == "" { + return name + } + + name = tmpName } if ns.NoLowerCase { diff --git a/schema/naming_test.go b/schema/naming_test.go index c3e6bf92..1fdab9a0 100644 --- a/schema/naming_test.go +++ b/schema/naming_test.go @@ -197,3 +197,14 @@ func TestFormatNameWithStringLongerThan64Characters(t *testing.T) { t.Errorf("invalid formatted name generated, got %v", formattedName) } } + +func TestReplaceEmptyTableName(t *testing.T) { + ns := NamingStrategy{ + SingularTable: true, + NameReplacer: strings.NewReplacer("Model", ""), + } + tableName := ns.TableName("Model") + if tableName != "Model" { + t.Errorf("invalid table name generated, got %v", tableName) + } +}