gpt4 book ai didi

typescript - QueryBuilder 的 TypeORM WHERE 子句中的参数有什么问题?

转载 作者:行者123 更新时间:2023-12-04 01:45:54 26 4
gpt4 key购买 nike

有人可以向我解释我在使用 where 子句的参数时做错了什么吗?

下一个 block 给了我下面的错误:

@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;
}
}

更新:示例 repo用于复制和typeorm issue在github上。

最佳答案

原始查询的问题是参数名称id被多次使用:

    .where('something.userId = :id', {id: userId})
.andWhere('something.id = :id',{id: spotId}).getOne();

根据 docs 中的注释,这些必须是唯一的.

关于typescript - QueryBuilder 的 TypeORM WHERE 子句中的参数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55240553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com