mysql - 如何在typeorm和nestjs中检索具有两个条件的数据
问题描述
下面的课是我的桌子
@Entity()
export class Comment extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column()
body: string
@Column()
createdAt: Date
@Column()
userId: number
@Column()
courseId: number
@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
}
我想检索parentId
等于 null 且coureseId
等于45的项目,
但它不起作用,我错在哪里?
public async getCommentsByCourseId(courseId: number): Promise<Comment[]> {
const query = this.createQueryBuilder('comment')
.innerJoinAndSelect('comment.children', 'parent')
.where("comment.parentId IS NULL")
.andWhere("comment.courseId = :courseId", { courseId: 45 }) // this line does not work
const comments = await query.getMany()
return comments
}
解决方案
我为一个虚拟实体尝试了以下操作 -
async getRecordsByUserId(userId: number): Promise<any[]> {
return await getRepository(DummyEntity)
.createQueryBuilder('dummyEntity')
.innerJoinAndSelect('dummyEntity.user', 'user')
.where("dummyEntity.entity_id IS NULL")
.andWhere("dummyEntity.userId = :userId", { userId: 1 })
.getRawMany();
}
它按预期工作。
推荐阅读
- java - 通过Java为soapUI不同名称的方法生成相同的XML请求标签
- python - 更改 matplotlib 中现有子图的子图尺寸
- php - Facebook 分析 - 获取 Facebook 帖子的分享数?
- c# - 从表单创建客户端计算机和服务器计算机之间的 SQL 服务器连接
- jquery - 为什么 revokeObjectURL 不显示 IE 11 上的文件大小
- authentication - 如何在注册页面 Odoo 中添加字段“状态”?
- javascript - 为什么我无法访问我的 JS 对象属性?
- node.js - 如何开发一个改变产品页面的shopify应用程序?
- python - 创建一个由函数内部的类组成并用于主要部分的系统
- c# - 在 wcf 服务中使用 Newtonsoft.Json 得到错误