nestjs-paginate icon indicating copy to clipboard operation
nestjs-paginate copied to clipboard

problem with view entity with composite primary key

Open mortezakarimi opened this issue 11 months ago • 4 comments
trafficstars

When we want to run pagination on the view entity without ID, and has 2 primary keys, when active load relation on primary keys relation, the result data field is empty but total items is correct

@ViewEntity({
  name: 'projects_and_simulated_projects',
  materialized: false,
  expression: `
         // .... some query
    `,
  dependsOn: [Project, Simulation],
})
export class ProjectsWithSimulatedProjects {
  @ViewColumn({
    name: 'project_id',
  })
  @PrimaryColumn({
    name: 'project_id',
  })
  @ManyToOne(
    () => Project,
    (project) => project.id,
    { eager: false },
  )
  @JoinColumn({ name: 'project_id', referencedColumnName: 'id' })
  project: Project;

  @ViewColumn({
    name: 'simulation_id',
  })
  @PrimaryColumn({
    name: 'simulation_id',
  })
  @ManyToOne(
    () => Simulation,
    (simulation) => simulation.id,
    { eager: false },
  )
  @JoinColumn({ name: 'simulation_id', referencedColumnName: 'id' })
  simulation: Simulation;

  //... other fields that are shared between the project and simulation entity
}

in the above view entity when we try to run pagination without loading relations everything works fine, but when loading relations with query builder or pagination config the result has the actual total number of items but the data property is an empty array

I tried to debug the query and figured out that the query only ran using the second primaryColumn field

also, make eager true not working and only id returned in the field

mortezakarimi avatar Dec 08 '24 13:12 mortezakarimi

possibly related to the snake case column names?

try this: https://github.com/ppetzold/nestjs-paginate/issues/641#issuecomment-1707477638

ppetzold avatar Dec 08 '24 16:12 ppetzold

No, I use typeorm-naming-strategies for my column names but it is not working with/without it

mortezakarimi avatar Dec 09 '24 12:12 mortezakarimi

have you checked the generated SQL? does it look alright?

ppetzold avatar Jan 04 '25 17:01 ppetzold

@ppetzold Yes

I tried to debug the query and figured out that the query only ran using the second primaryColumn field

mortezakarimi avatar Jan 05 '25 06:01 mortezakarimi