首页 > 解决方案 > 如何使用 typeorm nestjs 加入 3 个关系表

问题描述

我正在将 typeorm 与 NestJs 一起使用,我尝试加入 3 个关系但出现错误 path comment.user in entity was not found

这是我的表用户

ID 用户名
1
 @PrimaryGeneratedColumn()
  id: number;
  @Column()
  username: string;
  @OneToMany(() => PostEntity, (post) => post.user, { eager: true })
  posts: PostEntity[];

这是我的桌子

ID 描述 用户身份
1 文本 1
 @PrimaryGeneratedColumn()
  id: number;
  @Column()
  desc: string;
  @Column()
  userId: number;

  @ManyToOne(() => User, (user) => user.posts, {
    eager: false,
    onDelete: 'CASCADE',
  })
  user: User[];
  @OneToMany(() => CommentEntity, (comment: CommentEntity) => comment.post, {
    eager: true,
    onDelete: 'CASCADE',
  })
  comment: CommentEntity[];

这是我的表评论

ID 评论 用户身份 postId
1 评论帖 1 1 1
 @PrimaryGeneratedColumn()
  id: number;
  @Column()
  comment: string;
  @Column()
  userId: number;
  @Column()
  postId: number;
  @ManyToOne(() => PostEntity, () => (post: PostEntity) => post.comment, {
    eager: false,
    onDelete: 'CASCADE',
  })

  post: PostEntity[];
  @OneToOne(() => User)
  @JoinColumn()
  user: User;

在这里我使用 OneToOne 因为 1 条评论只能由 1 位用户评论

这就是我的数据的样子

```
this.createQueryBuilder('post')
      .leftJoinAndSelect('post.user', 'user')
      .leftJoinAndSelect('post.comment', 'comment')
      .select(['post', 'user.id', 'user.username', 'comment'])
      .getMany();
```

这就是我得到的

[
  {
    id: 1,
    description: 'comment post 1',
    userId: 1,
    user: {
      id: 1,
      username: 'row',
    },
    comment: [
      {
        id: 1,
        description: 'comment',
        userId: 1,
        postId: 1,
      },
    ],
  },
];

在评论中,我想为该评论的用户加入 userId。我希望数据看起来像这样

[
  {
    id: 1,
    description: 'comment post 1',
    userId: 1,
    user: {
      id: 1,
      username: 'row',
    },
    comment: [
      {
        id: 1,
        description: 'comment',
        userId: 1,
        postId: 1,
        user: {
          // if different user comment will show different username
          id: 1, 
          username: 'row',
        },
      },
    ],
  },
];

这就是我尝试做的

this.createQueryBuilder('post')
      .leftJoinAndSelect('post.user', 'user')
      .leftJoinAndSelect('post.comment', 'comment')
      .leftJoinAndSelect('post.comment.user', 'user') 
      .select(['post', 'user.id', 'user.username', 'comment'])
      .orderBy('post.updated_at', 'DESC')
      .getMany();


 .leftJoinAndSelect('post.comment.user', 'user') // In this line I want to join userId but Its show an error  path comment.user in entity was not found

更新

我尝试使用它(这意味着 PostEntity)

this.find({ relations: ['user', 'comment', 'comment.user']

但仍然无法正常工作

标签: mysqlexpressnestjstypeorm

解决方案


像这样更新您的Comment实体的用户关系:

@OneToOne(() => User)
@JoinColumn({name: 'userId'})
user: User;

然后尝试:

this.createQueryBuilder('post')
      .leftJoinAndSelect('post.user', 'user')
      .leftJoinAndSelect('post.comment', 'comment')
      .leftJoinAndSelect('comment.user', 'commentedUser') 
      .select(['post', 'user.id', 'user.username', 'comment', 'commentedUser'])
      .orderBy('post.updated_at', 'DESC')
      .getMany();

推荐阅读