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

Preload relationship does not work #1019

Open
anhc0de opened this issue Mar 25, 2024 · 0 comments
Open

Preload relationship does not work #1019

anhc0de opened this issue Mar 25, 2024 · 0 comments

Comments

@anhc0de
Copy link

anhc0de commented Mar 25, 2024

Package version

20.4.0

Describe the bug

I have set up relationships as below

User.ts

  @hasMany(() => Task, {
    foreignKey: 'assignedToId',
  })
  declare assignedTasks: HasMany<typeof Task>

Task.ts

 @column({ columnName: 'assigned_to_id' })
  declare assignedToId: number
  @belongsTo(() => User, {
    foreignKey: 'assignedToId',
  })
  declare assignedTo: BelongsTo<typeof User>

Query

const records = await Task.query()
      .preload('assignedTo')
      .orderBy('updated_at', 'desc')
      .orderBy('created_at', 'desc')
      .paginate(page, limit)

records.forEach((user) => {
      logger.info('assignedToId=%s preload::assignedTo=%o', user.assignedToId, user.assignedTo)
    })

Result

CleanShot 2024-03-25 at 16 42 59@2x

DB tables

User

CleanShot 2024-03-25 at 16 44 22@2x

Task

CleanShot 2024-03-25 at 16 45 06@2x

Reproduction repo

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant