diff --git a/src/packages/database/query/index.js b/src/packages/database/query/index.js index 561b5d79..236ce871 100644 --- a/src/packages/database/query/index.js +++ b/src/packages/database/query/index.js @@ -2,6 +2,7 @@ import { camelize } from 'inflection'; import entries from '../../../utils/entries'; +import uniq from '../../../utils/uniq'; import type Model from '../model'; import scopesFor from './utils/scopes-for'; @@ -162,12 +163,13 @@ class Query<+T: any> extends Promise { if (columnName) { this.snapshots = this.snapshots - .filter(([method]) => method !== 'orderBy') + .filter(([method]) => method !== 'orderByRaw') .concat([ - ['orderBy', [ - `${this.model.tableName}.${columnName}`, - direction - ]] + // eslint-disable-next-line prefer-template + ['orderByRaw', uniq([columnName, this.model.primaryKey]) + .map(key => `${this.model.tableName}.${key}`) + .join(', ') + ` ${direction}` + ] ]); } } @@ -222,7 +224,9 @@ class Query<+T: any> extends Promise { first(): this { if (!this.shouldCount) { - const willSort = this.snapshots.some(([method]) => method === 'orderBy'); + const willSort = this.snapshots.some( + ([method]) => method === 'orderByRaw' + ); this.collection = false; @@ -238,7 +242,9 @@ class Query<+T: any> extends Promise { last(): this { if (!this.shouldCount) { - const willSort = this.snapshots.some(([method]) => method === 'orderBy'); + const willSort = this.snapshots.some( + ([method]) => method === 'orderByRaw' + ); this.collection = false; @@ -399,7 +405,7 @@ class Query<+T: any> extends Promise { if (scopes.length) { const keys = scopes.map(scope => { if (scope === 'order') { - return 'orderBy'; + return 'orderByRaw'; } return scope; diff --git a/src/packages/database/test/query.test.js b/src/packages/database/test/query.test.js index 3863890c..20b4882e 100644 --- a/src/packages/database/test/query.test.js +++ b/src/packages/database/test/query.test.js @@ -335,7 +335,7 @@ describe('module "database/query"', () => { const result = subject.order('id', 'DESC'); expect(result.snapshots).to.deep.equal([ - ['orderBy', ['posts.id', 'DESC']] + ['orderByRaw', 'posts.id DESC'] ]); }); @@ -343,7 +343,7 @@ describe('module "database/query"', () => { const result = subject.order('id'); expect(result.snapshots).to.deep.equal([ - ['orderBy', ['posts.id', 'ASC']] + ['orderByRaw', 'posts.id ASC'] ]); }); @@ -440,7 +440,7 @@ describe('module "database/query"', () => { const result = subject.first(); expect(result.snapshots).to.deep.equal([ - ['orderBy', ['posts.id', 'ASC']], + ['orderByRaw', 'posts.id ASC'], ['limit', 1] ]); }); @@ -455,7 +455,7 @@ describe('module "database/query"', () => { const result = subject.order('createdAt', 'DESC').first(); expect(result.snapshots).to.deep.equal([ - ['orderBy', ['posts.created_at', 'DESC']], + ['orderByRaw', 'posts.created_at, posts.id DESC'], ['limit', 1] ]); }); @@ -494,7 +494,7 @@ describe('module "database/query"', () => { const result = subject.last(); expect(result.snapshots).to.deep.equal([ - ['orderBy', ['posts.id', 'DESC']], + ['orderByRaw', 'posts.id DESC'], ['limit', 1] ]); }); @@ -509,7 +509,7 @@ describe('module "database/query"', () => { const result = subject.order('createdAt', 'DESC').last(); expect(result.snapshots).to.deep.equal([ - ['orderBy', ['posts.created_at', 'DESC']], + ['orderByRaw', 'posts.created_at, posts.id DESC'], ['limit', 1] ]); });