如何使用 typeorm nestjs 加入 3 个关系 table
How to join 3 relation table using typeorm nestjs
我在 NestJs 中使用 typeorm,我尝试加入 3 个关系但出现错误 path comment.user in entity was not found
这是我的 table 用户
Id
username
1
row
@PrimaryGeneratedColumn()
id: number;
@Column()
username: string;
@OneToMany(() => PostEntity, (post) => post.user, { eager: true })
posts: PostEntity[];
这是我的 table 个帖子
Id
desc
user_id
1
text
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[];
这是我的 table 评论
Id
comment
user_id
postId
1
comment post 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();
我在 NestJs 中使用 typeorm,我尝试加入 3 个关系但出现错误 path comment.user in entity was not found
这是我的 table 用户
Id | username |
---|---|
1 | row |
@PrimaryGeneratedColumn()
id: number;
@Column()
username: string;
@OneToMany(() => PostEntity, (post) => post.user, { eager: true })
posts: PostEntity[];
这是我的 table 个帖子
Id | desc | user_id |
---|---|---|
1 | text | 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[];
这是我的 table 评论
Id | comment | user_id | postId |
---|---|---|---|
1 | comment post 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();