Can someone explain to me what I am doing wrong when using the parameters for my where clause?
This next block gives me the error below it:
@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
This request gives me the right result.
@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;
}
}
Update: Example repo for reproduction and typeorm issue on github.
The issue with the original query is that the parameter name id was used more than once:
.where('something.userId = :id', {id: userId})
.andWhere('something.id = :id',{id: spotId}).getOne();
These need to be unique according to this note in the docs.
The solution is to load the relations of my entity. As I understand it.
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;
}
Thanks to @Mukyuu for all your effort to help me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With