Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply relationship constraints when using sub query in whereIn condition #1037

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
Loading