Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!