diff --git a/main_test.go b/main_test.go index 34f96a86..af64119a 100644 --- a/main_test.go +++ b/main_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "reflect" "strconv" + "sync" "testing" "time" @@ -23,6 +24,7 @@ import ( var ( DB *gorm.DB t1, t2, t3, t4, t5 time.Time + mu sync.RWMutex ) func init() { @@ -262,6 +264,38 @@ func TestTableName(t *testing.T) { DB.SingularTable(false) } +func TestTableNameWithTableNameHandler(t *testing.T) { + mu.Lock() + orig := gorm.DefaultTableNameHandler + defer func() { + mu.Unlock() + gorm.DefaultTableNameHandler = orig + }() + gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string { + return "renamed_" + defaultTableName + } + + if DB.NewScope(Cart{}).TableName() != "renamed_shopping_cart" { + t.Errorf("Cart's table name should be renamed_shopping_cart") + } + + if DB.NewScope(&Cart{}).TableName() != "renamed_shopping_cart" { + t.Errorf("&Cart's table name should be renamed_shopping_cart") + } + + var cart interface{} + + cart = Cart{} + if DB.NewScope(cart).TableName() != "renamed_shopping_cart" { + t.Errorf("interface{}'s table name should be renamed_shopping_cart") + } + + cart = &Cart{} + if DB.NewScope(cart).TableName() != "renamed_shopping_cart" { + t.Errorf("interface{}'s table name should be renamed_shopping_cart") + } +} + func TestNullValues(t *testing.T) { DB.DropTable(&NullValue{}) DB.AutoMigrate(&NullValue{}) diff --git a/scope.go b/scope.go index 51ebd5a0..1c8bacfd 100644 --- a/scope.go +++ b/scope.go @@ -314,14 +314,6 @@ func (scope *Scope) TableName() string { return scope.Search.tableName } - if tabler, ok := scope.Value.(tabler); ok { - return tabler.TableName() - } - - if tabler, ok := scope.Value.(dbTabler); ok { - return tabler.TableName(scope.db) - } - return scope.GetModelStruct().TableName(scope.db.Model(scope.Value)) }