Typescript 使用TypeORM获取自引用关系中的数据
这是我的实体:Typescript 使用TypeORM获取自引用关系中的数据,typescript,typeorm,self-referencing-table,Typescript,Typeorm,Self Referencing Table,这是我的实体: @Entity() export class Comment extends BaseEntity { @PrimaryGeneratedColumn() id: number @Column({ type: "text" }) body: string @Column() displayName: string @OneToMany(() => Comment, comment =>
@Entity()
export class Comment extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column({ type: "text" })
body: string
@Column()
displayName: string
@OneToMany(() => Comment, comment => comment.parent, { eager: true })
children: Comment[];
@Column({ nullable: true })
parentId: number
@ManyToOne(() => Comment, comment => comment.children)
@JoinColumn({ name: 'parentId' })
parent: Comment
@Column()
status: CommentStatus
}
export enum CommentStatus {
Visible = 1,
InVisible = 2
}
伪造数据:
id parentId status body
----------------------------------------
1 NULL 1 body-1
----------------------------------------
2 1 1 body-1-1
----------------------------------------
3 1 2 body-1-2
----------------------------------------
4 1 2 body-1-3
我希望检索具有以下条件的行:
parentId
为NULL
和status
为CommentStatus。可见
const query = this.createQueryBuilder('comment')
.leftJoinAndSelect('comment.children', 'parent')
.where("comment.parentId IS NULL")
.andWhere("comment.status = :status", { status: CommentStatus.Visible })
const comments = await query.getMany()
它检索所有行,因为它不检查子项的状态
,为什么
非常感谢您的帮助您需要为家长及其子女添加两个不同的where子句 比如说-
const query = this.createQueryBuilder('comment')
.leftJoinAndSelect('comment.children', 'parent')
.where("comment.parentId IS NULL")
.andWhere("comment.status = :status", { status: CommentStatus.Visible })
.andWhere("parent.status = :status", { status: CommentStatus.Visible })
const comments = await query.getMany()