Don't use instance setting for order_by_primary_key

This commit is contained in:
Jinzhu 2015-03-23 11:07:39 +08:00
parent 1e28551d25
commit 7d16055a5d
2 changed files with 3 additions and 3 deletions

View File

@ -21,7 +21,7 @@ func Query(scope *Scope) {
dest = reflect.Indirect(reflect.ValueOf(value))
}
if orderBy, ok := scope.InstanceGet("gorm:order_by_primary_key"); ok {
if orderBy, ok := scope.Get("gorm:order_by_primary_key"); ok {
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
scope.Search.Order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), primaryKey, orderBy))
}

View File

@ -193,14 +193,14 @@ func (s *DB) Assign(attrs ...interface{}) *DB {
func (s *DB) First(out interface{}, where ...interface{}) *DB {
newScope := s.clone().NewScope(out)
newScope.Search.Limit(1)
return newScope.InstanceSet("gorm:order_by_primary_key", "ASC").
return newScope.Set("gorm:order_by_primary_key", "ASC").
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
}
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
newScope := s.clone().NewScope(out)
newScope.Search.Limit(1)
return newScope.InstanceSet("gorm:order_by_primary_key", "DESC").
return newScope.Set("gorm:order_by_primary_key", "DESC").
inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
}