Skip to content

fix: fix up generated query with .update() - #11993

Merged
gioboa merged 3 commits into
typeorm:masterfrom
gioboa:fix/11629
Feb 15, 2026
Merged

gioboa merged 3 commits into
typeorm:masterfrom
gioboa:fix/11629

Conversation

@gioboa

@gioboa gioboa commented Feb 14, 2026

Copy link
Copy Markdown
Collaborator

Close #11629

Description of change

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as Fixes #00000
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

User description

Close #11629

Description of change

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as Fixes #00000
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

PR Type

Bug fix


Description

  • Fix generated query to use equality operator instead of IN for single primary key updates

  • Import Equal operator for single-value comparisons

  • Add JSDoc parameter documentation to multiple methods

  • Add test case for UUID primary key update behavior


Diagram Walkthrough

flowchart LR
  A["whereInIds with single ID"] --> B["Check if single value"]
  B --> C["Use Equal operator instead of IN"]
  C --> D["Generate correct SQL with ="]
Loading

File Walkthrough

Relevant files
Bug fix
QueryBuilder.ts
Fix single ID update queries and improve documentation     

src/query-builder/QueryBuilder.ts

  • Import Equal operator from find-options
  • Refactor getWhereInIdsCondition to use Equal operator for single-value
    conditions instead of wrapping in In
  • Extract normalized values into variable for cleaner logic
  • Add JSDoc parameter documentation to 20+ methods for improved code
    documentation
+42/-6   
Tests
UuidPost.ts
Add UUID test entity for update tests                                       

test/functional/repository/update-by-criteria/entity/UuidPost.ts

  • Create new test entity UuidPost with UUID primary key
  • Define entity with auto-generated UUID id and title column
  • Used for testing update behavior with UUID primary keys
+14/-0   
repository-update-by-criteria.test.ts
Add test for UUID primary key update behavior                       

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts

  • Import new UuidPost test entity
  • Add comprehensive test case for single UUID primary key updates
  • Verify generated query uses equality operator instead of IN
  • Validate correct row is updated and others remain unchanged
+41/-0   

@pkg-pr-new

pkg-pr-new Bot commented Feb 14, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@11993
    

commit: 89d2785

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Feb 14, 2026 •

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to 89d2785

CategorySuggestion                                                                                                                                    Impact
Possible issue
Prevent invalid empty-id SQL

Add a check for an empty ids array to prevent generating invalid SQL. If the
array is empty, return a condition that is always false, such as 1=0.

src/query-builder/QueryBuilder.ts [1277-1283]

-const values = normalized.map((id) =>
-    primaryColumn.getEntityValue(id, false),
-)
+const values = normalized
+    .map((id) => primaryColumn.getEntityValue(id, false))
+    .filter((v) => v !== undefined)
+
+if (values.length === 0) {
+    return new Brackets((qb) => qb.where("1=0"))
+}
+
 return {
     [primaryColumn.propertyName]:
         values.length === 1 ? Equal(values[0]) : In(values),
 }
  • Apply / Chat
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies an edge case where an empty ids array would cause the generation of invalid SQL (IN ()), leading to a runtime error. The proposed fix is a standard and robust way to handle this scenario.

Medium
General
Assert operator in WHERE

Refine the test assertion to specifically check for the = operator within the
WHERE clause, rather than anywhere in the query, to prevent false positives from
the SET clause.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts [197-199]

 const query = qb.getQuery()
-expect(query).to.not.match(/\bIN\s*\(/)
-expect(query).to.contain("=")
+const wherePart = query.split(/\bWHERE\b/i)[1] ?? ""
 
+expect(wherePart).to.not.match(/\bIN\s*\(/i)
+expect(wherePart).to.match(/=\s*[:?]/)
+
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly points out that the test assertion is too broad and could pass for the wrong reason. By making the assertion specific to the WHERE clause, the test becomes more robust and accurately verifies the intended change.

Low
  • More

Previous suggestions

✅ Suggestions up to commit a3d626e
CategorySuggestion                                                                                                                                    Impact
Possible issue
Handle empty id lists safely

In getWhereInIdsCondition, add a check to handle empty ids arrays to prevent
generating invalid SQL. If the array is empty, return a condition that always
evaluates to false.

src/query-builder/QueryBuilder.ts [1277-1283]

 const values = normalized.map((id) =>
     primaryColumn.getEntityValue(id, false),
 )
+
+if (values.length === 0) {
+    return { "0": Equal(1) } as any
+}
+
 return {
     [primaryColumn.propertyName]:
         values.length === 1 ? Equal(values[0]) : In(values),
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies a bug where passing an empty array to whereInIds would generate invalid SQL, leading to runtime errors, and proposes a standard and effective fix.

Medium
✅ Make test assert WHERE equality
Suggestion Impact:The commit partially implemented the suggestion by replacing the weak "not contain IN" check with a regex that matches an IN( pattern. It did not implement the improved WHERE-specific equality assertion or the case-insensitive flags.

code diff:

                 const query = qb.getQuery()
-                expect(query).to.not.contain("IN")
+                expect(query).to.not.match(/\bIN\s*\(/)
                 expect(query).to.contain("=")

Improve the test assertion to specifically check for an = operator within the
WHERE clause of the generated query, rather than just its presence anywhere in
the query string.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts [199-201]

 const query = qb.getQuery()
-expect(query).to.not.match(/\bIN\s*\(/)
-expect(query).to.contain("=")
+expect(query).to.not.match(/\bIN\s*\(/i)
+expect(query).to.match(/\bWHERE\b[\s\S]*=\s*[:?]/i)
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly points out that the test assertion is too weak and could produce a false positive, proposing a more specific check to ensure the test is robust and validates the intended behavior correctly.

Medium
Fix strict property initialization

Add definite assignment assertions (!) to the id and title properties in the
UuidPost entity to prevent TypeScript compilation errors when
strictPropertyInitialization is enabled.

test/functional/repository/update-by-criteria/entity/UuidPost.ts [6-12]

 export class UuidPost {
     @PrimaryColumn("uuid")
-    id: string
+    id!: string
 
     @Column()
-    title: string
+    title!: string
 }
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies a potential TypeScript compilation error under strictPropertyInitialization and proposes a standard fix for TypeORM entities, improving code quality and preventing future build issues.

Low
Suggestions up to commit 89d2785
CategorySuggestion                                                                                                                                    Impact
Possible issue
Guard against empty id lists

Add a check to handle empty ID arrays in getWhereInIdsCondition to prevent
generating invalid SQL, by using IN (NULL) instead of IN ().

src/query-builder/QueryBuilder.ts [1277-1283]

 const values = normalized.map((id) =>
     primaryColumn.getEntityValue(id, false),
 )
+
+if (values.length === 0) {
+    return { [primaryColumn.propertyName]: In([null as any]) }
+}
+
 return {
     [primaryColumn.propertyName]:
         values.length === 1 ? Equal(values[0]) : In(values),
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies an edge case where an empty array of IDs would generate invalid SQL (IN ()), and provides a valid fix to prevent runtime errors.

Medium
Assert equals in WHERE clause

Improve the test assertion to specifically check for an equals sign (=) within
the WHERE clause, as the current check could be satisfied by the SET clause.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts [197-199]

 const query = qb.getQuery()
 expect(query).to.not.match(/\bIN\s*\(/)
-expect(query).to.contain("=")
+expect(query).to.match(/\bWHERE\b[\s\S]*\s=\s/)
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly points out that the test assertion is too weak and could pass even if the implementation is incorrect, proposing a more robust check to improve test quality.

Medium
✅ Suggestions up to commit 577a206
CategorySuggestion                                                                                                                                    Impact
Possible issue
Handle empty id lists safely

In getWhereInIdsCondition, add a check for an empty ids array to prevent
generating invalid SQL, and return an always-false condition instead.

src/query-builder/QueryBuilder.ts [1279-1285]

 const values = normalized.map((id) =>
     primaryColumn.getEntityValue(id, false),
 )
+if (values.length === 0) {
+    return new Brackets((qb) => qb.where("1=0"))
+}
 return {
     [primaryColumn.propertyName]:
         values.length === 1 ? Equal(values[0]) : In(values),
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies an edge case where an empty ids array would generate invalid SQL (... IN ()), proposing a robust fix that prevents potential runtime errors.

Medium
General
✅ Strengthen query-shape assertion
Suggestion Impact:The test assertion was strengthened by switching from a simple string containment check for "IN" to a regex that specifically matches an IN clause pattern (IN followed by an opening parenthesis), aligning with the suggestion's intent to reduce fragility. The suggested WHERE-clause-only parsing and equality predicate regex were not implemented.

code diff:

                 const query = qb.getQuery()
-                expect(query).to.not.contain("IN")
+                expect(query).to.not.match(/\bIN\s*\(/)
                 expect(query).to.contain("=")

Refine the test assertions to specifically check the WHERE clause for an
equality predicate and the absence of IN, making the test more precise and less
fragile.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts [197-199]

 const query = qb.getQuery()
-expect(query).to.not.match(/\bIN\s*\(/)
-expect(query).to.contain("=")
+const wherePart = query.split(/\bWHERE\b/i)[1] ?? ""
+expect(wherePart).to.not.match(/\bIN\s*\(/i)
+expect(wherePart).to.match(/\b=\s*[:?]/)
Suggestion importance[1-10]: 5

__

Why: The suggestion improves the test's robustness by making the assertions more specific and less likely to pass or fail for unintended reasons, which enhances test quality.

Low
✅ Suggestions up to commit bd8c4d2
CategorySuggestion                                                                                                                                    Impact
Possible issue
Handle empty id lists safely

Add a guard clause to handle empty ids arrays in getWhereInIdsCondition to
prevent generating invalid SQL. If the ids array is empty, return a condition
that always evaluates to false.

src/query-builder/QueryBuilder.ts [1263-1287]

 const normalized = (Array.isArray(ids) ? ids : [ids]).map((id) =>
     metadata.ensureEntityIdMap(id),
 )
+
+// avoid generating invalid SQL for empty ids (e.g. `IN ()`)
+if (normalized.length === 0) {
+    return new Brackets((qb) => qb.where("0=1"))
+}
 
 // using in(...ids) for single primary key entities
 if (!metadata.hasMultiplePrimaryKeys) {
     const primaryColumn = metadata.primaryColumns[0]
 
     // getEntityValue will try to transform `In`, it is a bug
     // todo: remove this transformer check after #2390 is fixed
     // This also fails for embedded & relation, so until that is fixed skip it.
     if (
         !primaryColumn.transformer &&
         !primaryColumn.relationMetadata &&
         !primaryColumn.embeddedMetadata
     ) {
         const values = normalized.map((id) =>
             primaryColumn.getEntityValue(id, false),
         )
         return {
             [primaryColumn.propertyName]:
                 values.length === 1 ? Equal(values[0]) : In(values),
         }
     }
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies a bug where an empty ids array would lead to invalid SQL (IN ()), causing a runtime error. The proposed fix prevents this crash and improves the method's robustness.

Medium
✅ Assert equality in WHERE clause
Suggestion Impact:The commit updated the "no IN" assertion from a simple substring check to a regex matching an IN clause pattern. It did not implement the suggested refinement to assert "=" specifically within the WHERE clause (nor the case-insensitive flags).

code diff:

                 const query = qb.getQuery()
-                expect(query).to.not.contain("IN")
+                expect(query).to.not.match(/\bIN\s*\(/)
                 expect(query).to.contain("=")

Refine the test assertion to specifically check for an equals sign (=) within
the WHERE clause, not just anywhere in the query, to avoid false positives from
the SET clause.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts [197-199]

 const query = qb.getQuery()
-expect(query).to.not.match(/\bIN\s*\(/)
-expect(query).to.contain("=")
+expect(query).to.not.match(/\bIN\s*\(/i)
+expect(query).to.match(/\bWHERE\b[\s\S]*=\s*/i)
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly points out a flaw in the test assertion that could lead to a false positive, as an UPDATE query's SET clause will always contain an = character. Improving the test makes it more reliable.

Medium
✅ Suggestions up to commit 577a206
CategorySuggestion                                                                                                                                    Impact
Possible issue
Handle empty ids safely

Add a check for an empty ids array in getWhereInIdsCondition to prevent
generating invalid SQL by returning an always-false predicate.

src/query-builder/QueryBuilder.ts [1279-1285]

 const values = normalized.map((id) =>
     primaryColumn.getEntityValue(id, false),
 )
+if (values.length === 0) {
+    return new Brackets((qb) => qb.where("1=0"))
+}
 return {
     [primaryColumn.propertyName]:
         values.length === 1 ? Equal(values[0]) : In(values),
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies an edge case where an empty ids array could lead to invalid SQL (IN ()), proposing a robust fix that prevents potential runtime errors.

Medium
Fix strict property initialization

Add definite assignment assertions (!) to the id and title properties in the
UuidPost entity to prevent potential strictPropertyInitialization errors.

test/functional/repository/update-by-criteria/entity/UuidPost.ts [7-14]

 export class UuidPost {
     @PrimaryColumn("uuid")
     @Generated("uuid")
-    id: string
+    id!: string
 
     @Column()
-    title: string
+    title!: string
 }
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies a potential TypeScript compilation error under strictPropertyInitialization and provides a valid fix, improving the robustness of the test entity.

Low
General
✅ Assert against the WHERE clause
Suggestion Impact:The test assertion for avoiding IN usage was tightened from a broad string check (`to.not.contain("IN")`) to a regex that specifically matches the `IN (` pattern, aligning with part of the suggestion (though it did not scope assertions to the WHERE clause or refine the "=" check).

code diff:

                 const query = qb.getQuery()
-                expect(query).to.not.contain("IN")
+                expect(query).to.not.match(/\bIN\s*\(/)
                 expect(query).to.contain("=")

Refine the test assertion to specifically check for the = operator within the
WHERE clause, avoiding false positives from the SET clause.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts [197-199]

 const query = qb.getQuery()
-expect(query).to.not.match(/\bIN\s*\(/)
-expect(query).to.contain("=")
+const wherePart = query.split(/\bWHERE\b/i)[1] ?? ""
+expect(wherePart).to.not.match(/\bIN\s*\(/i)
+expect(wherePart).to.match(/=\s*[:?]/)
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly points out a flaw in the test assertion that could lead to a false positive, and the proposed improvement makes the test more robust and accurate.

Low

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Feb 14, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider


Remediation recommended

✅ 1. Empty @PARAM JSDoc tags 📘 Rule violation ✓ Correctness
Description
Multiple JSDoc blocks add @param tags with no descriptions, which reads as autogenerated comment
noise and adds no value. This conflicts with the requirement to avoid AI-like/low-signal comments
and keep style consistent.
Code

src/query-builder/QueryBuilder.ts[R105-106]

+     * @param connectionOrQueryBuilder
+     * @param queryRunner
Evidence
PR Compliance ID 4 prohibits introducing comment noise or AI-like additions. The added JSDoc
@param entries provide only parameter names without any descriptive content, which is low-signal
and repeated across the file.

Rule 4: Remove AI-generated noise
src/query-builder/QueryBuilder.ts[105-106]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
New JSDoc `@param` tags were added without any parameter descriptions (only names), creating low-value comment noise.
## Issue Context
The compliance checklist requires avoiding AI-generated/low-signal comments and keeping style consistent.
## Fix Focus Areas
- src/query-builder/QueryBuilder.ts[105-106]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Advisory comments

✅ 2. Brittle SQL substring assert 🐞 Bug ⛯ Reliability
Description
The new test asserts the generated SQL does not contain the substring "IN", which can be brittle if
future query formatting/keywords or identifiers include "IN" even when the WHERE operator is
correct. This is low risk because the test also validates actual update behavior, but tightening the
assertion would reduce flakiness.
Code

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts[R197-200]

+                const query = qb.getQuery()
+                expect(query).to.not.contain("IN")
+                expect(query).to.contain("=")
+
Evidence
The test uses broad substring matching on the whole SQL string, not specifically the WHERE
clause/operator, making it sensitive to unrelated SQL changes.

test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts[197-200]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The test checks `qb.getQuery()` with `to.not.contain("IN")`, which can become flaky if the SQL text contains the substring `IN` for reasons unrelated to using the `IN` operator in the WHERE clause.
### Issue Context
The intent is to ensure single-id `whereInIds` generates `=` instead of `IN (...)`.
### Fix Focus Areas
- test/functional/repository/update-by-criteria/repository-update-by-criteria.test.ts[197-200]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@coveralls

coveralls commented Feb 14, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 81.14% (+0.005%) from 81.135%
when pulling 89d2785 on gioboa:fix/11629
into f5cea95 on typeorm:master.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 577a206

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit bd8c4d2

@gioboa
gioboa requested review from G0maa, alumni and naorpeled February 15, 2026 12:41
Comment thread src/query-builder/QueryBuilder.ts Resolved
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 577a206

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 8917288

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 89d2785

Comment thread src/query-builder/QueryBuilder.ts Resolved
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit a3d626e

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 89d2785

@gioboa
gioboa merged commit fe6c072 into typeorm:master Feb 15, 2026
74 checks passed
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

.update() generating a malformed query

5 participants