I have an Entity like this:
@Entity()
export class CourseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
@DeleteDateColumn()
deletedAt: Date;
@Column()
name: string;
@OneToMany(() => BoughtCourseEntity, (boughtCourse) => boughtCourse.course)
boughtCourses!: Array<BoughtCourseEntity>;
@Column({ select: false, insert: false, readonly: true, nullable: true })
boughtCoursesCount: number;
constructor(partial: Partial<CourseEntity>) {
Object.assign(this, partial);
}
}
In my queryBuilder
I assign a value to boughtCoursesCount
and then I want to do the ordering by this column but it does not work without any errors:
query.addSelect((subQuery) => {
return subQuery
.select('COUNT(bc.id)', 'boughtCoursesCount')
.from('bought_course_entity', 'bc')
.where('bc.courseId = course.id');
}, 'course_boughtCoursesCount');
And then I do orderBy('course.boughtCoursesCount')
. I guess the problem might be due to some naming conventions but not sure what could I do here.