diff --git a/gorm_test.go b/gorm_test.go index f704d2d4..d97e185d 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -1312,7 +1312,19 @@ func (c Cart) TableName() string { func TestTableName(t *testing.T) { db := db.clone() if db.do(Order{}).table() != "orders" { - t.Errorf("Order table name should be orders") + t.Errorf("Order's table name should be orders") + } + + if db.do(&Order{}).table() != "orders" { + t.Errorf("&Order's table name should be orders") + } + + if db.do([]Order{}).table() != "orders" { + t.Errorf("[]Order's table name should be orders") + } + + if db.do(&[]Order{}).table() != "orders" { + t.Errorf("&[]Order's table name should be orders") } db.SingularTable(true) @@ -1320,9 +1332,33 @@ func TestTableName(t *testing.T) { t.Errorf("Order's singular table name should be order") } + if db.do(&Order{}).table() != "order" { + t.Errorf("&Order's singular table name should be order") + } + + if db.do([]Order{}).table() != "order" { + t.Errorf("[]Order's singular table name should be order") + } + + if db.do(&[]Order{}).table() != "order" { + t.Errorf("&[]Order's singular table name should be order") + } + if db.do(&Cart{}).table() != "shopping_cart" { + t.Errorf("&Cart's singular table name should be shopping_cart") + } + + if db.do(Cart{}).table() != "shopping_cart" { t.Errorf("Cart's singular table name should be shopping_cart") } + + if db.do(&[]Cart{}).table() != "shopping_cart" { + t.Errorf("&[]Cart's singular table name should be shopping_cart") + } + + if db.do([]Cart{}).table() != "shopping_cart" { + t.Errorf("[]Cart's singular table name should be shopping_cart") + } db.SingularTable(false) } diff --git a/model.go b/model.go index d5fcf5ff..faab29ba 100644 --- a/model.go +++ b/model.go @@ -214,6 +214,11 @@ func (m *Model) tableName() (str string) { } data := m.reflectData() + + if data.Kind() == reflect.Slice { + data = reflect.New(data.Type().Elem()).Elem() + } + if fm := data.MethodByName("TableName"); fm.IsValid() { if v := fm.Call([]reflect.Value{}); len(v) > 0 { if result, ok := v[0].Interface().(string); ok {