adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.87k stars 638 forks source link

Nested Preload under BelongsTo Preload not running #4025

Closed nelsonjd closed 1 year ago

nelsonjd commented 1 year ago

Nested Preload function under belongsTo preload is not running.

Package version

"@adonisjs/auth": "^8.2.3",
"@adonisjs/bouncer": "^2.3.0",
"@adonisjs/core": "^5.9.0",
"@adonisjs/lucid": "^18.3.0",
"@adonisjs/mail": "^7.2.4",
"@adonisjs/repl": "^3.1.6",
"@adonisjs/session": "^6.4.0",
"@adonisjs/view": "^6.1.0",

Node.js and npm version

% node -v v16.16.0 % npm -v 8.11.0

Sample Code (to reproduce the issue)

const strategy = await StrategyRepository.findByUidForUser(params.strategyId)
public findByUidForUser(uid: string) {
    return Strategy.query()
      .where('uid', uid)
      .preload('account', (accountQuery) => {
        console.log('i ran')
        accountQuery.preload('users')
      })
      .firstOrFail()
  }

i ran is not being printed.

select * from "Strategies" where "uid" = ? limit ? [ 'btQ3a', 1 ]
select * from "Accounts" where "id" in (?) [ 1 ]

are SQL outputs

Strategy.ts

  @column()
  public accountId: number

  @belongsTo(() => Account)
  public account: BelongsTo<typeof Account>

Account.ts

  @manyToMany(() => User, {
    pivotTable: 'UserAccounts',
    pivotForeignKey: 'accountId',
    pivotRelatedForeignKey: 'userId',
  })
  public users: ManyToMany<typeof User>

BONUS (a sample repo to reproduce the issue)

Joan1590 commented 1 year ago

Hi, do you try to put first preload instead of where statement?

thetutlage commented 1 year ago

Please open issues in the correct repo