Skip to content

Commit

Permalink
fix: sql expression when where parameter is empty array (#9691)
Browse files Browse the repository at this point in the history
Closes: #9690

Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
  • Loading branch information
PronDmytro and AlexMesser committed Feb 6, 2023
1 parent 7726f5a commit 7df2ccf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4127,14 +4127,14 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
}

protected buildWhere(
where: FindOptionsWhere<any>,
where: FindOptionsWhere<any>[] | FindOptionsWhere<any>,
metadata: EntityMetadata,
alias: string,
embedPrefix?: string,
) {
let condition: string = ""
// let parameterIndex = Object.keys(this.expressionMap.nativeParameters).length;
if (Array.isArray(where)) {
if (Array.isArray(where) && where.length) {
condition =
"(" +
where
Expand Down
7 changes: 7 additions & 0 deletions test/github-issues/9690/entity/Foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class Foo {
@PrimaryGeneratedColumn({ name: "id" })
id: number
}
30 changes: 30 additions & 0 deletions test/github-issues/9690/issue-9690.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "reflect-metadata"
import { DataSource } from "../../../src"
import {
closeTestingConnections,
createTestingConnections,
} from "../../utils/test-utils"
import { Foo } from "./entity/Foo"

describe("github issues > #9690 Incorrect SQL expression if `where` parameter is empty array", () => {
let dataSources: DataSource[]
before(async () => {
dataSources = await createTestingConnections({
enabledDrivers: ["postgres"],
entities: [Foo],
schemaCreate: true,
dropSchema: true,
})
})
after(() => closeTestingConnections(dataSources))

it("should run without throw error", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const repository = dataSource.getRepository(Foo)
await repository.find({
where: [],
})
}),
))
})

0 comments on commit 7df2ccf

Please sign in to comment.