Typescript QueryBuilder的TypeForm WHERE子句中的参数有什么问题?
有人能向我解释一下在使用where子句的参数时我做错了什么吗 下一个块给出了下面的错误:Typescript QueryBuilder的TypeForm WHERE子句中的参数有什么问题?,typescript,query-builder,typeorm,Typescript,Query Builder,Typeorm,有人能向我解释一下在使用where子句的参数时我做错了什么吗 下一个块给出了下面的错误: @EntityRepository(Something) export class SomethingRepository extends Repository<Something>{ findByUserAndSomethingById(userId: number, spotId: number){ const thing = this.createQueryBuilder('
@EntityRepository(Something)
export class SomethingRepository extends Repository<Something>{
findByUserAndSomethingById(userId: number, spotId: number){
const thing = this.createQueryBuilder('something')
.where('something.userId = :id', {id: userId})
.andWhere('something.id = :id',{id: spotId}).getOne();
return thing;
}
}
这个请求给了我正确的结果
@EntityRepository(Something)
export class SomethingRepository extends Repository<Something>{
findByUserAndSomethingById(userId: number, spotId: number){
const thing = this.createQueryBuilder('something')
.where(`"something"."userId" = ${userId}`)
.andWhere('something.id = :id',{id: spotId}).getOne();
return thing;
}
}
@EntityRepository(某物)
导出类SomethingRepository扩展存储库{
findByUserAndSomethingById(userId:number,spotId:number){
const thing=this.createQueryBuilder('something')
.where(`something'.'userId'=${userId}`)
.andWhere('something.id=:id',{id:spotId}).getOne();
归还物;
}
}
更新:
复制和github上的示例。我不能肯定,因为它不在。但是,当我使用TypeORM QueryBuilder运行对SQL的查询时,通常需要在别名和字段名前后添加另一个引号 例如,在您的示例中,您需要使用:
.where(“'something.”userId“'=:id',{id:userId})
作为第二个示例中的使用方式:.where(“'something.”userId”=${userId})
调试的一种方法通常是检查执行失败的查询。是否所有查询都按照您正常执行的方式执行,或者是否缺少引号。解决方案是加载我的实体的关系。据我所知
findByUserAndSomethingById(userId: number, spotId: number) {
const thing = this.createQueryBuilder('something')
.innerJoin('something.user', 'user')
.where('user.id = :uid', { uid: userId })
.andWhere('something.id = :sid', { sid: spotId }).getOne();
return thing;
}
感谢@Mukyuu为我提供的所有帮助。原始查询的问题是参数名称
id
被多次使用:
.where('something.userId = :id', {id: userId})
.andWhere('something.id = :id',{id: spotId}).getOne();
根据中的注释,这些查询必须是唯一的。您是否能够读取项目中正在执行的查询?是的,我可以。看看这个要点,我想我知道这是从哪里来的。错误消息是
'column bathingspot.userid不存在'
提示是'也许您是想引用列“bathingspot.userid”
,似乎应用参数的字符串中的camelcase以某种方式是“uncamelcased”。我能注意到的唯一区别是在查询之前和之后的”
,在失败的查询和来自的成功查询之间。另外,工作查询中的2
中可能缺少$
<代码>其中“bathingspot”。“userId”=2和“bathingspot”。“id”=1美元。