mirror of https://github.com/go-gorm/gorm.git
Fix TableName for slices
This commit is contained in:
parent
dd77ca6df7
commit
fb05f2fde2
38
gorm_test.go
38
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)
|
||||
}
|
||||
|
||||
|
|
5
model.go
5
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 {
|
||||
|
|
Loading…
Reference in New Issue