Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 1656a11

Browse files
docs: add note about unique parameters in QueryBuilder (typeorm#7572)
When using the QueryBuilder, unique parameters need to be provided in WHERE expressions. This commit adds that information to the docs, as it's easy to overlook for users.
1 parent 7dbf683 commit 1656a11

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/select-query-builder.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Select using Query Builder
22

33
* [What is `QueryBuilder`](#what-is-querybuilder)
4+
* [Important note when using the `QueryBuilder`](#important-note-when-using-the-querybuilder)
45
* [How to create and use a `QueryBuilder`](#how-to-create-and-use-a-querybuilder)
56
* [Getting values using QueryBuilder](#getting-values-using-querybuilder)
67
* [What are aliases for?](#what-are-aliases-for)
@@ -62,6 +63,32 @@ User {
6263
}
6364
```
6465

66+
## Important note when using the `QueryBuilder`
67+
68+
When using the `QueryBuilder`, you need to provide unique parameters in your `WHERE` expressions. **This will not work**:
69+
70+
```TypeScript
71+
const result = await getConnection()
72+
.createQueryBuilder('user')
73+
.leftJoinAndSelect('user.linkedSheep', 'linkedSheep')
74+
.leftJoinAndSelect('user.linkedCow', 'linkedCow')
75+
.where('user.linkedSheep = :id', { id: sheepId })
76+
.andWhere('user.linkedCow = :id', { id: cowId });
77+
```
78+
79+
... but this will:
80+
81+
```TypeScript
82+
const result = await getConnection()
83+
.createQueryBuilder('user')
84+
.leftJoinAndSelect('user.linkedSheep', 'linkedSheep')
85+
.leftJoinAndSelect('user.linkedCow', 'linkedCow')
86+
.where('user.linkedSheep = :sheepId', { sheepId })
87+
.andWhere('user.linkedCow = :cowId', { cowId });
88+
```
89+
90+
Note that we uniquely named `:sheepId` and `:cowId` instead of using `:id` twice for different parameters.
91+
6592
## How to create and use a `QueryBuilder`
6693

6794
There are several ways how you can create a `Query Builder`:

0 commit comments

Comments
 (0)