fix: sql expression when where parameter is empty array (#9691)

Closes: #9690

Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
This commit is contained in:
PronDmytro 2023-02-06 19:15:32 +02:00 committed by GitHub
parent 7726f5ad1e
commit 7df2ccf69d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,7 @@
import { Entity, PrimaryGeneratedColumn } from "../../../../src"
@Entity()
export class Foo {
@PrimaryGeneratedColumn({ name: "id" })
id: number
}

View File

@ -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: [],
})
}),
))
})