From 5e62e7fdad2a56ccd57f67bc4831b3816c8d1779 Mon Sep 17 00:00:00 2001 From: Xavier Dumesnil Date: Thu, 10 Apr 2014 16:29:09 +0200 Subject: [PATCH 1/2] Include scope.TableName() in ORDER statement for First() & Last() --- main.go | 4 ++-- main_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 942880b5..c98ca2b6 100644 --- a/main.go +++ b/main.go @@ -131,13 +131,13 @@ func (s *DB) Assign(attrs ...interface{}) *DB { func (s *DB) First(out interface{}, where ...interface{}) *DB { scope := s.clone().NewScope(out) - scope.Search = scope.Search.clone().order(scope.PrimaryKey()).limit(1) + scope.Search = scope.Search.clone().order(scope.TableName()+"."+scope.PrimaryKey()).limit(1) return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db } func (s *DB) Last(out interface{}, where ...interface{}) *DB { scope := s.clone().NewScope(out) - scope.Search = scope.Search.clone().order(scope.PrimaryKey() + " DESC").limit(1) + scope.Search = scope.Search.clone().order(scope.TableName()+"."+scope.PrimaryKey() + " DESC").limit(1) return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db } diff --git a/main_test.go b/main_test.go index 2434c8ef..31771a2c 100644 --- a/main_test.go +++ b/main_test.go @@ -247,6 +247,18 @@ func TestFirstAndLast(t *testing.T) { } } +func TestFirstAndLastWithJoins(t *testing.T) { + var user1, user2, user3, user4 User + db.Joins("left join emails on emails.user_id = users.id").First(&user1) + db.Order("id").Find(&user2) + + db.Joins("left join emails on emails.user_id = users.id").Last(&user3) + db.Order("id desc").Find(&user4) + if user1.Id != user2.Id || user3.Id != user4.Id { + t.Errorf("First and Last should works correctly") + } +} + func TestFirstAndLastForTableWithNoStdPrimaryKey(t *testing.T) { var animal1, animal2, animal3, animal4 Animal db.First(&animal1) From 2b7306aca1a250bf53984eed68954334d84fb9d6 Mon Sep 17 00:00:00 2001 From: Xavier Dumesnil Date: Fri, 11 Apr 2014 09:58:23 +0200 Subject: [PATCH 2/2] Fix typos --- main_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/main_test.go b/main_test.go index 31771a2c..e4016407 100644 --- a/main_test.go +++ b/main_test.go @@ -237,7 +237,7 @@ func TestFirstAndLast(t *testing.T) { db.Last(&user3) db.Order("id desc").Find(&user4) if user1.Id != user2.Id || user3.Id != user4.Id { - t.Errorf("First and Last should works correctly") + t.Errorf("First and Last should work correctly") } var users []User @@ -255,7 +255,7 @@ func TestFirstAndLastWithJoins(t *testing.T) { db.Joins("left join emails on emails.user_id = users.id").Last(&user3) db.Order("id desc").Find(&user4) if user1.Id != user2.Id || user3.Id != user4.Id { - t.Errorf("First and Last should works correctly") + t.Errorf("First and Last should work correctly with Joins") } } @@ -267,7 +267,7 @@ func TestFirstAndLastForTableWithNoStdPrimaryKey(t *testing.T) { db.Last(&animal3) db.Order("counter desc").Find(&animal4) if animal1.Counter != animal2.Counter || animal3.Counter != animal4.Counter { - t.Errorf("First and Last should works correctly") + t.Errorf("First and Last should work correctly") } var animals []Animal @@ -631,7 +631,7 @@ func TestOrderAndPluck(t *testing.T) { 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") + t.Errorf("Reorder should work") } var names []string @@ -648,7 +648,7 @@ func TestLimit(t *testing.T) { db.Order("age desc").Limit(3).Find(&users1).Limit(5).Find(&users2).Limit(-1).Find(&users3) if len(users1) != 3 || len(users2) != 5 || len(users3) <= 5 { - t.Errorf("Limit should works") + t.Errorf("Limit should work") } } @@ -657,7 +657,7 @@ func TestOffset(t *testing.T) { db.Limit(100).Order("age desc").Find(&users1).Offset(3).Find(&users2).Offset(5).Find(&users3).Offset(-1).Find(&users4) if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) { - t.Errorf("Offset should works") + t.Errorf("Offset should work") } } @@ -674,7 +674,7 @@ func TestCount(t *testing.T) { var users []User if err := db.Where("name = ?", "1").Or("name = ?", "3").Find(&users).Count(&count).Error; err != nil { - t.Errorf("Count should works", err) + t.Errorf("Count should work", err) } if count != int64(len(users)) { @@ -683,7 +683,7 @@ func TestCount(t *testing.T) { db.Model(&User{}).Where("name = ?", "1").Count(&count1).Or("name = ?", "3").Count(&count2) if count1 != 1 || count2 != 3 { - t.Errorf("Multiple count should works") + t.Errorf("Multiple count should work") } } @@ -801,7 +801,7 @@ func TestRunCallbacks(t *testing.T) { var products []Product db.Find(&products, "code = ?", "unique_code") if products[0].AfterFindCallTimes != 2 { - t.Errorf("AfterFind callbacks should works with slice") + t.Errorf("AfterFind callbacks should work with slice") } db.Where("Code = ?", "unique_code").First(&p) @@ -1765,7 +1765,7 @@ func TestScan(t *testing.T) { var res result db.Table("users").Select("name, age").Where("name = ?", 3).Scan(&res) if res.Name != "3" { - t.Errorf("Scan into struct should works") + t.Errorf("Scan into struct should work") } var ress []result