首页 > 解决方案 > Typeorm 一对多关系和树

问题描述

您好,我有Post实体:

@Entity()
export class Post extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @OneToMany(() => Comment, (comment) => comment.post)
  comments: Comment[];
}

评论实体:

@Entity()
@Tree('closure-table')
export class Comment extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({ type: 'text' })
  body: string;

  @TreeParent()
  parent: Comment;

  @TreeChildren()
  children: Comment[];

  @ManyToOne(() => Post, (post) => post.comments)
  post: Post;
}

帖子与评论有一对多关系,评论有树关系。

我想获取具有树关系的 id 为 1 的帖子的评论,但不知道该怎么做!

我可以以树的方式获取评论,但无法以树的方式获取帖子评论。

标签: javascriptnode.jstypescripttypeorm

解决方案


推荐阅读