From c2c1dd1fc80c16f466ab889a29c787840b7c6d21 Mon Sep 17 00:00:00 2001 From: Jay Taylor Date: Tue, 23 Jun 2015 15:27:21 -0700 Subject: [PATCH 1/3] Fix errors being inaccessible due to errors being set on different *DB instance than what is returned. --- main.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index c3a1629c..ff7182bf 100644 --- a/main.go +++ b/main.go @@ -396,28 +396,33 @@ func (s *DB) AutoMigrate(values ...interface{}) *DB { } func (s *DB) ModifyColumn(column string, typ string) *DB { - s.clone().NewScope(s.Value).modifyColumn(column, typ) - return s + scope := s.clone().NewScope(s.Value) + scope.modifyColumn(column, typ) + return scope.db } func (s *DB) DropColumn(column string) *DB { - s.clone().NewScope(s.Value).dropColumn(column) - return s + scope := s.clone().NewScope(s.Value) + scope.dropColumn(column) + return scope.db } func (s *DB) AddIndex(indexName string, column ...string) *DB { - s.clone().NewScope(s.Value).addIndex(false, indexName, column...) - return s + scope := s.clone().NewScope(s.Value) + scope.addIndex(false, indexName, column...) + return scope.db } func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB { - s.clone().NewScope(s.Value).addIndex(true, indexName, column...) - return s + scope := s.clone().NewScope(s.Value) + scope.addIndex(true, indexName, column...) + return scope.db } func (s *DB) RemoveIndex(indexName string) *DB { - s.clone().NewScope(s.Value).removeIndex(indexName) - return s + scope := s.clone().NewScope(s.Value) + scope.removeIndex(indexName) + return scope.db } /* @@ -427,7 +432,8 @@ Example: db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT") */ func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB { - s.clone().NewScope(s.Value).addForeignKey(field, dest, onDelete, onUpdate) + scope := s.clone().NewScope(s.Value) + scope.addForeignKey(field, dest, onDelete, onUpdate) return s } From bdb6fc55e8bb267450a99dc722c9d69dfe814298 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 24 Jun 2015 13:56:30 +0800 Subject: [PATCH 2/3] 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") From 2a1d64c3e067bd1af99987157f9625a4605ed85e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 24 Jun 2015 14:09:59 +0800 Subject: [PATCH 3/3] Return cloned db instance for AddForeignKey --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index ff7182bf..aba51fc4 100644 --- a/main.go +++ b/main.go @@ -434,7 +434,7 @@ Example: func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB { scope := s.clone().NewScope(s.Value) scope.addForeignKey(field, dest, onDelete, onUpdate) - return s + return scope.db } func (s *DB) Association(column string) *Association {