Skip to content

Commit

Permalink
fix: do not update table name for model query builder when already as…
Browse files Browse the repository at this point in the history
…signed

FIXES #738
  • Loading branch information
thetutlage committed Nov 9, 2021
1 parent 88d2272 commit c62ff03
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions test/database/query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
47 changes: 47 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit c62ff03

Please sign in to comment.