【TypeORM】プレースホルダーを使う時の地雷
環境
再現コード
function findOneByBarAndId(
bar: Bar
id: number // foo の id (コード内参照)
): Promise<Foo | undefined> {
const queryBuilder = this.fooRepo
.createQueryBuilder('foo')
.innerJoinAndSelect('foo.bars', 'bar')
.where('foo.id = :id', { id })
.andWhere('bar.id = :id', { id: bar.id }) // ここに地雷があります
.getOne()
}
地雷
function findOneByBarAndId(
bar: Bar
id: number // foo の id (コード内参照)
): Promise<Foo | undefined> {
const queryBuilder = this.fooRepo
.createQueryBuilder('foo')
.innerJoinAndSelect('foo.bars', 'bar')
.where('foo.id = :id', { id })
.andWhere('bar.id = :id', { id: bar.id }) // ここに地雷があります
.getOne()
}
地雷
id
が被っているため、前の id
の値も後の値(ここでは bar.id
)が使われてしまいます。
最初のプレースホルダー部分を { id: id }
としても駄目です。
DBのログを有効(logging: true
)にして、 同じ値が使われていたのを見て気が付きました。
地雷回避方法
プレースホルダー名を変えましょう。
- .andWhere('bar.id = :id', { id: bar.id }) // 地雷
+ .andWhere('bar.id = :barId', { barId: bar.id }) // 回避
ドキュメント
https://github.com/typeorm/typeorm/blob/master/docs/select-query-builder.md#using-subqueries
に書かれていました。
Note: do not use the same parameter name for different values across the query builder. Values will be overridden if you set them multiple times.
Author And Source
この問題について(【TypeORM】プレースホルダーを使う時の地雷), 我々は、より多くの情報をここで見つけました https://qiita.com/piggydev/items/86fd4f67bafe6e05ae1b著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .