Fix TableName for slices

This commit is contained in:
Jinzhu 2013-12-31 09:45:01 +08:00
parent dd77ca6df7
commit fb05f2fde2
2 changed files with 42 additions and 1 deletions

View File

@ -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)
}

View File

@ -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 {