Skip to content

Commit

Permalink
fix: replace empty table name result in panic (go-gorm#5048)
Browse files Browse the repository at this point in the history
* fix: replace empty name result in panic

* fix: replace empty table name result in panic
  • Loading branch information
li-jin-gou authored Feb 8, 2022
1 parent 416c4d0 commit d222151
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion schema/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions schema/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d222151

Please sign in to comment.