Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Pagination without id column in TypeORM

Is there any way of selecting only given columns without id column and using pagination (skip, take) in TypeORM. I'm doing this in NestJS:

article.service.ts:

async getAll(skip: number): Promise<Article[]> {
  return await this.articleRepository.find({
    select: {
      id: false,
      title: true,
      description: true,
      body: false,
      created_at: true,
      updated_at: false,
      author: {
        full_name: true
      }
    },
    relations: {
      author: true
    },
    skip: skip,
    take: 10,
    order: {
      created_at: 'DESC'
    }
  });
}

And in article.controller.ts:

@Get('articles')
async getAll(@Query('page') page: number) {
  if(!page) { page = 1; }
  const skip = (page - 1) * 10;

  const articles = await this.articlesService.getAll(skip);
  return {
    status: true,
    articles: articles,
    page: page,
  }
}

My article.entity.ts:

@Entity()
export class Article {
  
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 100 })
  title: string;

  @Column()
  description: string

  @Column({ type: 'text' })
  body: string;

  @ManyToOne(() => User, (user) => user.articles)
  author: User;

  @CreateDateColumn()
  created_at: string;

  @UpdateDateColumn()
  updated_at: string;
}

When I send a request to this endpoint error happens. I think this is because of pagination(skip, take).

QueryFailedError: column distinctAlias.Article_id does not exist
    at PostgresQueryRunner.query (project/src/driver/postgres/PostgresQueryRunner.ts:299:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at SelectQueryBuilder.loadRawResults (project/src/query-builder/SelectQueryBuilder.ts:3601:25)
    at SelectQueryBuilder.getRawMany (project/src/query-builder/SelectQueryBuilder.ts:1573:29)
    at SelectQueryBuilder.executeEntitiesAndRawResults (project/src/query-builder/SelectQueryBuilder.ts:3295:26)
    at SelectQueryBuilder.getRawAndEntities (project/src/query-builder/SelectQueryBuilder.ts:1617:29)
    at SelectQueryBuilder.getMany (project/src/query-builder/SelectQueryBuilder.ts:1707:25)
    at ArticlesService.getAll (project/src/articles/articles.service.ts:35:12)
    at ArticlesController.getAll (project/src/articles/articles.controller.ts:56:22)
    at project/node_modules/@nestjs/core/router/router-execution-context.js:46:28

But if I remove skip and take or select the id column it works. Is there any way that I can select other columns without id column and use pagination together

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Solved issue temporarily by selecting id columns and removing them before returning

articles.forEach(article => { delete article.id });

Here is the full function from article.service.ts:

async getAll(skip: number): Promise<Article[]> {
  const articles = await this.articleRepository.find({
    select: {
      id: true,
      title: true,
      description: true,
      body: false,
      created_at: true,
      updated_at: false,
      author: {
        full_name: true
      }
    },
    relations: {
      author: true
    },
    skip: skip,
    take: 10,
    order: {
      created_at: 'DESC'
    }
  });
  articles.forEach(article => { delete article.id });
  return articles;
}

NOTE: There should be a better solution though.

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda