【发布时间】:2021-09-25 01:49:34
【问题描述】:
您好,这是我的 comment.entity.ts:
@Entity()
export class Comment extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
email: string;
@Column({ type: 'text' })
body: string;
@Column()
type: CommentTypeEnum.POST | CommentTypeEnum.COMMENT;
@Column({ type: 'boolean', default: false })
has_parent: boolean;
@ManyToMany(() => Comment, (comment) => comment.children)
parent: Comment;
@OneToMany(() => Comment, (comment) => comment.parent)
children: Comment[];
}
如你所见,我有一对多关系的自引用。
我这样获取数据:
const query = Comment.createQueryBuilder('comment');
query.andWhere('comment.has_parent = false');
const comments = await query.orderBy('created_at', 'DESC').getMany();
我想在结果中添加孩子数,我不知道该怎么做。
【问题讨论】:
-
所以,如果我理解正确,你想获得所有
Comment(s) 其中has_parent是false并计算有多少childrenthis实体有? -
@CarloCorradini 是的人
标签: javascript node.js typescript nestjs typeorm