Skip to content

Commit

Permalink
Add add limit 1 for Find automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 12, 2015
1 parent df33db6 commit 4c815fd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 0 additions & 2 deletions callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func Query(scope *Scope) {
isPtr = true
destType = destType.Elem()
}
} else {
scope.Search = scope.Search.clone().limit(1)
}

scope.prepareQuerySql()
Expand Down
8 changes: 4 additions & 4 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func TestFirstAndLast(t *testing.T) {

var user1, user2, user3, user4 User
DB.First(&user1)
DB.Order("id").Find(&user2)
DB.Order("id").Limit(1).Find(&user2)

DB.Last(&user3)
DB.Order("id desc").Find(&user4)
DB.Order("id desc").Limit(1).Find(&user4)
if user1.Id != user2.Id || user3.Id != user4.Id {
t.Errorf("First and Last should by order by primary key")
}
Expand All @@ -41,10 +41,10 @@ func TestFirstAndLastWithNoStdPrimaryKey(t *testing.T) {

var animal1, animal2, animal3, animal4 Animal
DB.First(&animal1)
DB.Order("counter").Find(&animal2)
DB.Order("counter").Limit(1).Find(&animal2)

DB.Last(&animal3)
DB.Order("counter desc").Find(&animal4)
DB.Order("counter desc").Limit(1).Find(&animal4)
if animal1.Counter != animal2.Counter || animal3.Counter != animal4.Counter {
t.Errorf("First and Last should work correctly")
}
Expand Down

0 comments on commit 4c815fd

Please sign in to comment.