Improve compatibility when set model

This commit is contained in:
Jinzhu 2013-11-01 11:43:41 +08:00
parent 32dcacc70b
commit 600d8e7277
2 changed files with 4 additions and 5 deletions

View File

@ -404,20 +404,20 @@ func TestOrderAndPluck(t *testing.T) {
}
var ages1, ages2 []int64
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages1).Order("age").Pluck("age", &ages2)
db.Model([]User{}).Order("age desc").Pluck("age", &ages1).Order("age").Pluck("age", &ages2)
if !reflect.DeepEqual(ages1, ages2) {
t.Errorf("The first order is the primary order")
}
var ages3, ages4 []int64
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages3).Order("age", true).Pluck("age", &ages4)
db.Model(&User{}).Order("age desc").Pluck("age", &ages3).Order("age", true).Pluck("age", &ages4)
if reflect.DeepEqual(ages3, ages4) {
t.Errorf("Reorder should works well")
}
ages = []int64{}
var names []string
db.Model(&User{}).Order("name").Order("age desc").Pluck("age", &ages).Pluck("name", &names)
db.Model(User{}).Order("name").Order("age desc").Pluck("age", &ages).Pluck("name", &names)
if !(names[0] == "1" && names[2] == "3" && names[3] == "3" && ages[2] == 24 && ages[3] == 22) {
t.Errorf("Should be ordered correctly with multiple orders")
}

View File

@ -34,8 +34,7 @@ func (m *Model) primaryKeyValue() int64 {
if m.data == nil {
return -1
}
data := reflect.ValueOf(m.data).Elem()
data := reflect.Indirect(reflect.ValueOf(m.data))
switch data.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice: