mysql - 如何使用 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']
但仍然无法正常工作
解决方案
像这样更新您的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();
推荐阅读
- php - PHP 仅在包含页面上加载页面
- java - 我没有得到这样的元素异常:运行我的项目时找不到文件
- laravel-5 - 使用 Laravel 的索引匹配导入 csv
- javascript - 从 D3 line() 获取点数组
- excel - VBA 字节值正确写入二进制文件,但在回读到用户窗体时显示不同
- scheme - 当函数调用两次时,let 中定义的变量发生了变化
- python-2.7 - 在 python 上下载文件被禁止 http 403
- angular - 无法在模板驱动表单的下拉菜单中设置默认值
- powershell - 如何在 Powershell 中等待线程?
- android - 使用 Glide 和 RecyclerView 上下滚动时图像消失,图像之间的空间增加