diff --git a/test/integration/builder/selects.js b/test/integration/builder/selects.js index 6c89e47023..61cae6f850 100644 --- a/test/integration/builder/selects.js +++ b/test/integration/builder/selects.js @@ -1,6 +1,7 @@ /*global expect, d*/ 'use strict'; +const mysqlErrors = require('mysql/lib/protocol/constants/errors'); const _ = require('lodash'); const assert = require('assert'); const Runner = require('../../../lib/runner'); @@ -1380,5 +1381,40 @@ module.exports = function(knex) { } } }); + + it('select from subquery', async function() { + const subquery = knex.from('accounts').whereBetween('id', [3, 5]); + return knex + .pluck('id') + .orderBy('id') + .from(subquery) + .then( + (rows) => { + expect(rows).to.deep.equal([3, 4, 5]); + expect(knex.client.driverName).to.oneOf(['sqlite3', 'oracledb']); + }, + (e) => { + switch (knex.client.driverName) { + case 'mysql': + case 'mysql2': + expect(e.errno).to.equal( + mysqlErrors.ER_DERIVED_MUST_HAVE_ALIAS + ); + break; + case 'pg': + expect(e.message).to.contain('must have an alias'); + break; + + case 'mssql': + expect(e.message).to.contain( + "Incorrect syntax near the keyword 'order'" + ); + break; + default: + throw e; + } + } + ); + }); }); };