nestjsx / nestjs-typeorm-paginate

:page_with_curl: Pagination response object function + types for typeorm + nestjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ER_DUP_FIELDNAME while using querybuilder with join in v3.1.3

shashi-3dqr opened this issue · comments

The below error occurs while using a query builder with join (tried inner and left join). And, this occurs only in v3.1.3 and not in v3.1.2.

code: 'ER_DUP_FIELDNAME',
errno: 1060,
sqlState: '42S21',
sqlMessage: "Duplicate column name 'id'",
query: 'SELECT COUNT(*) AS `value` FROM (SELECT * FROM `code` `c` INNER JOIN `user` `u` ON `u`.`id`=`c`.`userId` ORDER BY `c`.`id` DESC) `uniqueTableAlias`',
  parameters: []

I don't see a duplication? That's odd. Mind sharing with me your pagination func usage?

const paginationOptions = {
  page: 1,
  limit: 10,
  route: '',
  routingLabels: { limitLabel: 'itemsPerPage', pageLabel: 'currentPage' }
};

const queryBuilder = this.codesRepository
  .createQueryBuilder('c')
  .innerJoinAndSelect('c.user', 'u')
  .orderBy('c.id', 'DESC');
        
 const page = await paginate<CodeEntity>(
   queryBuilder,
   paginationOptions,
 );

Entities:

export class CodeEntity extends AbstractEntity {
  @Column()
  text: string;

  @ManyToOne(() => UserEntity, { onDelete: 'CASCADE' })
  user: UserEntity;

  constructor(entity?: Partial<CodeEntity>) {
    super(entity);
  }
}

export class UserEntity extends AbstractEntity {
  @Column({ unique: true, nullable: true })
  username: string;

  @Column({ unique: true })
  @IsEmail()
  email: string;

  @Column()
  password: string;

  constructor(entity?: Partial<UserEntity>) {
    super(entity);
  }
}

export abstract class AbstractEntity {
  @PrimaryGeneratedColumn({ type: 'int', unsigned: false })
  id: number;

  @CreateDateColumn({ nullable: true })
  createdAt: Date;

  @UpdateDateColumn({ nullable: true })
  updatedAt: Date;

  protected constructor(entity?: Partial<AbstractEntity>) {
    if (entity) {
      Object.assign(this, entity);
    }
  }
}

ahhh I see it now. SELECT * is not aliases. Annoying