mirror of https://github.com/go-gorm/gorm.git
Merge pull request #90 from xdmnl/master
Fix ORDER BY "id" is ambiguous
This commit is contained in:
commit
65137121ff
4
main.go
4
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
|
||||
}
|
||||
|
||||
|
|
30
main_test.go
30
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
|
||||
|
@ -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 work correctly with Joins")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstAndLastForTableWithNoStdPrimaryKey(t *testing.T) {
|
||||
var animal1, animal2, animal3, animal4 Animal
|
||||
db.First(&animal1)
|
||||
|
@ -255,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
|
||||
|
@ -619,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
|
||||
|
@ -636,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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -645,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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -662,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)) {
|
||||
|
@ -671,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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -789,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)
|
||||
|
@ -1753,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
|
||||
|
|
Loading…
Reference in New Issue