diff --git a/src/Database/QueryBuilder/Database.ts b/src/Database/QueryBuilder/Database.ts index 48c3be14..341081f3 100644 --- a/src/Database/QueryBuilder/Database.ts +++ b/src/Database/QueryBuilder/Database.ts @@ -304,6 +304,7 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil .clearOffset() .clearSelect() .count('* as total') + const aggregates = await countQuery.exec() const total = this.hasGroupBy ? aggregates.length : aggregates[0].total diff --git a/src/Orm/QueryBuilder/index.ts b/src/Orm/QueryBuilder/index.ts index 63aa1c10..82a68178 100644 --- a/src/Orm/QueryBuilder/index.ts +++ b/src/Orm/QueryBuilder/index.ts @@ -150,7 +150,13 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon customFn, model.$keys.attributesToColumns.resolve.bind(model.$keys.attributesToColumns) ) - builder.table(model.table) + + /** + * Assign table when not already assigned + */ + if (!builder['_single'] || !builder['_single'].table) { + builder.table(model.table) + } } /** diff --git a/test/database/query-builder.spec.ts b/test/database/query-builder.spec.ts index 71b350de..83a87fed 100644 --- a/test/database/query-builder.spec.ts +++ b/test/database/query-builder.spec.ts @@ -10777,6 +10777,46 @@ test.group('Query Builder | paginate', (group) => { await connection.disconnect() }) + + test('use table aliases', async (assert) => { + const connection = new Connection('primary', getConfig(), app.logger) + connection.connect() + + const db = getQueryBuilder(getQueryClient(connection, app)) + const usersList = getUsers(18) + await getInsertBuilder(getQueryClient(connection, app)).table('users').multiInsert(usersList) + + const users = await db + .from({ u: 'users' }) + .where('u.username', usersList[0].username) + .paginate(1, 5) + + users.baseUrl('/users') + + assert.lengthOf(users.all(), 1) + assert.equal(users.perPage, 5) + assert.equal(users.currentPage, 1) + assert.equal(users.lastPage, 1) + assert.isFalse(users.hasPages) + assert.isFalse(users.hasMorePages) + assert.isFalse(users.isEmpty) + assert.equal(users.total, 1) + assert.isTrue(users.hasTotal) + + assert.deepEqual(users.getMeta(), { + total: 1, + per_page: 5, + current_page: 1, + last_page: 1, + first_page: 1, + first_page_url: '/users?page=1', + last_page_url: '/users?page=1', + next_page_url: null, + previous_page_url: null, + }) + + await connection.disconnect() + }) }) test.group('Query Builder | clone', (group) => { diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 4eff4383..0aff1b79 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -5734,6 +5734,53 @@ test.group('Base Model | paginate', (group) => { previousPageUrl: null, }) }) + + test('use table aliases', async (assert) => { + class User extends BaseModel { + @column({ isPrimary: true }) + public id: number + + @column() + public username: string + + @column() + public email: string + } + + const usersList = getUsers(18) + await db.insertQuery().table('users').multiInsert(usersList) + + const users = await User.query() + .from({ u: User.table }) + .where('u.username', usersList[0].username) + .paginate(1, 5) + + users.baseUrl('/users') + + assert.instanceOf(users, ModelPaginator) + + assert.lengthOf(users.all(), 1) + assert.instanceOf(users.all()[0], User) + assert.equal(users.perPage, 5) + assert.equal(users.currentPage, 1) + assert.equal(users.lastPage, 1) + assert.isFalse(users.hasPages) + assert.isFalse(users.hasMorePages) + assert.isFalse(users.isEmpty) + assert.equal(users.total, 1) + assert.isTrue(users.hasTotal) + assert.deepEqual(users.getMeta(), { + total: 1, + per_page: 5, + current_page: 1, + last_page: 1, + first_page: 1, + first_page_url: '/users?page=1', + last_page_url: '/users?page=1', + next_page_url: null, + previous_page_url: null, + }) + }) }) test.group('Base Model | toObject', (group) => {