Skip to content

Commit

Permalink
fix: apply constraints in whereIn condition
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcikado committed Jun 13, 2024
1 parent 44da18c commit 19346b8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/database/query_builder/chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ export abstract class Chainable extends Macroable implements ChainableContract {
*/
protected transformValue(value: any) {
if (value instanceof Chainable) {
value.applyWhere()
return value.knexQuery
return value.toKnex()
}

if (value instanceof ReferenceBuilder) {
Expand Down Expand Up @@ -2074,4 +2073,12 @@ export abstract class Chainable extends Macroable implements ChainableContract {

return this
}

/**
* Applies statements and returns knex query
*/
toKnex() {
this.applyWhere()
return this.knexQuery
}
}
8 changes: 8 additions & 0 deletions src/orm/relations/base/query_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ export abstract class BaseQueryBuilder
return this
}

/**
* Return knex query
*/
toKnex() {
this.applyConstraints()
return super.toKnex()
}

/**
* Get query sql
*/
Expand Down
50 changes: 49 additions & 1 deletion test/orm/model_query_builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { test } from '@japa/runner'
import { AppFactory } from '@adonisjs/core/factories/app'

import { column } from '../../src/orm/decorators/index.js'
import { column, hasMany } from '../../src/orm/decorators/index.js'
import { scope } from '../../src/orm/base_model/index.js'
import { ModelQueryBuilder } from '../../src/orm/query_builder/index.js'
import {
Expand All @@ -21,6 +21,7 @@ import {
resetTables,
getBaseModel,
} from '../../test-helpers/index.js'
import { HasMany } from '../../src/types/relations.js'

test.group('Model query builder', (group) => {
group.setup(async () => {
Expand Down Expand Up @@ -443,4 +444,51 @@ test.group('Model query builder', (group) => {
const users = await User.query().count('* as total')
assert.equal(Number(users[0].$extras.total), 2)
})

test('apply relationship constraints when using sub query', async ({ fs, assert }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
await app.init()
const db = getDb()
const adapter = ormAdapter(db)
const BaseModel = getBaseModel(adapter)

class Post extends BaseModel {
@column()
declare userId: number | null

@column()
declare title: string
}

class User extends BaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare username: string

@hasMany(() => Post)
declare posts: HasMany<typeof Post>
}

Post.boot()
User.boot()

const users = await User.createMany([
{
username: 'virk',
},
{
username: 'nikk',
},
])

for (let user of users) {
await user.related('posts').create({ title: 'Test' })
}

const posts = await Post.query().whereIn('id', users[0].related('posts').query().select('id'))

assert.lengthOf(posts, 1)
})
})

0 comments on commit 19346b8

Please sign in to comment.