From bdb6fc55e8bb267450a99dc722c9d69dfe814298 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 24 Jun 2015 13:56:30 +0800 Subject: [PATCH] Overwrite slice results with Find --- association_test.go | 2 -- callback_query.go | 2 ++ query_test.go | 12 ------------ 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/association_test.go b/association_test.go index 3ffd8880..ea5b1b80 100644 --- a/association_test.go +++ b/association_test.go @@ -148,7 +148,6 @@ func TestManyToMany(t *testing.T) { t.Errorf("Query many to many relations") } - newLanguages = []Language{} DB.Model(&user).Association("Languages").Find(&newLanguages) if len(newLanguages) != len([]string{"ZH", "EN"}) { t.Errorf("Should be able to find many to many relations") @@ -194,7 +193,6 @@ func TestManyToMany(t *testing.T) { t.Errorf("Language EE should not be deleted") } - languages = []Language{} DB.Where("name IN (?)", []string{"CC", "DD"}).Find(&languages) user2 := User{Name: "Many2Many_User2", Languages: languages} diff --git a/callback_query.go b/callback_query.go index 59022eba..4de911e8 100644 --- a/callback_query.go +++ b/callback_query.go @@ -30,6 +30,8 @@ func Query(scope *Scope) { if kind := dest.Kind(); kind == reflect.Slice { isSlice = true destType = dest.Type().Elem() + dest.Set(reflect.Indirect(reflect.New(reflect.SliceOf(destType)))) + if destType.Kind() == reflect.Ptr { isPtr = true destType = destType.Elem() diff --git a/query_test.go b/query_test.go index d84fae93..b15d01ba 100644 --- a/query_test.go +++ b/query_test.go @@ -98,49 +98,41 @@ func TestSearchWithPlainSQL(t *testing.T) { t.Errorf("Should found 2 users that age > 1, but got %v", len(users)) } - users = []User{} DB.Where("name LIKE ?", "%PlainSqlUser%").Where("age >= ?", 1).Find(&users) if len(users) != 3 { t.Errorf("Should found 3 users that age >= 1, but got %v", len(users)) } - users = []User{} scopedb.Where("age <> ?", 20).Find(&users) if len(users) != 2 { t.Errorf("Should found 2 users age != 20, but got %v", len(users)) } - users = []User{} scopedb.Where("birthday > ?", now.MustParse("2000-1-1")).Find(&users) if len(users) != 2 { t.Errorf("Should found 2 users's birthday > 2000-1-1, but got %v", len(users)) } - users = []User{} scopedb.Where("birthday > ?", "2002-10-10").Find(&users) if len(users) != 2 { t.Errorf("Should found 2 users's birthday >= 2002-10-10, but got %v", len(users)) } - users = []User{} scopedb.Where("birthday >= ?", "2010-1-1").Where("birthday < ?", "2020-1-1").Find(&users) if len(users) != 1 { t.Errorf("Should found 1 users's birthday < 2020-1-1 and >= 2010-1-1, but got %v", len(users)) } - users = []User{} DB.Where("name in (?)", []string{user1.Name, user2.Name}).Find(&users) if len(users) != 2 { t.Errorf("Should found 2 users, but got %v", len(users)) } - users = []User{} DB.Where("id in (?)", []int64{user1.Id, user2.Id, user3.Id}).Find(&users) if len(users) != 3 { t.Errorf("Should found 3 users, but got %v", len(users)) } - users = []User{} DB.Where("id in (?)", user1.Id).Find(&users) if len(users) != 1 { t.Errorf("Should found 1 users, but got %v", len(users)) @@ -191,7 +183,6 @@ func TestSearchWithStruct(t *testing.T) { t.Errorf("Search first record with where struct") } - users = []User{} DB.Find(&users, &User{Name: user2.Name}) if len(users) != 1 { t.Errorf("Search all records with inline struct") @@ -222,7 +213,6 @@ func TestSearchWithMap(t *testing.T) { t.Errorf("Search all records with inline map") } - users = []User{} DB.Find(&users, map[string]interface{}{"name": user3.Name}) if len(users) != 1 { t.Errorf("Search all records with inline map") @@ -395,13 +385,11 @@ func TestNot(t *testing.T) { t.Errorf("Should find all users's name not equal 3") } - users4 = []User{} DB.Not("name = ?", "user3").Find(&users4) if len(users1)-len(users4) != int(name3Count) { t.Errorf("Should find all users's name not equal 3") } - users4 = []User{} DB.Not("name <> ?", "user3").Find(&users4) if len(users4) != int(name3Count) { t.Errorf("Should find all users's name not equal 3")