|
1 | 1 | # Select using Query Builder
|
2 | 2 |
|
3 | 3 | * [What is `QueryBuilder`](#what-is-querybuilder)
|
| 4 | +* [Important note when using the `QueryBuilder`](#important-note-when-using-the-querybuilder) |
4 | 5 | * [How to create and use a `QueryBuilder`](#how-to-create-and-use-a-querybuilder)
|
5 | 6 | * [Getting values using QueryBuilder](#getting-values-using-querybuilder)
|
6 | 7 | * [What are aliases for?](#what-are-aliases-for)
|
@@ -62,6 +63,32 @@ User {
|
62 | 63 | }
|
63 | 64 | ```
|
64 | 65 |
|
| 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 | + |
65 | 92 | ## How to create and use a `QueryBuilder`
|
66 | 93 |
|
67 | 94 | There are several ways how you can create a `Query Builder`:
|
|
0 commit comments