QueryBuilder 的 TypeORM WHERE 子句中的参数有什么问题?
What is wrong with the parameters in my TypeORM WHERE clause for the QueryBuilder?
有人可以向我解释一下我在为我的 where 子句使用参数时做错了什么吗?
下一个块给出了下面的错误:
@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;
}
}
QueryFailedError: column something.userid does not exist
这个请求给了我正确的结果。
@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;
}
}
更新:
github.
上的示例 repo for reproduction and typeorm issue
我不能肯定,因为它不在 documentation 中。但是当我使用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();
根据 docs 中的注释,这些必须是唯一的。
有人可以向我解释一下我在为我的 where 子句使用参数时做错了什么吗?
下一个块给出了下面的错误:
@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;
}
}
QueryFailedError: column something.userid does not exist
这个请求给了我正确的结果。
@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;
}
}
更新: github.
上的示例 repo for reproduction and typeorm issue我不能肯定,因为它不在 documentation 中。但是当我使用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();
根据 docs 中的注释,这些必须是唯一的。