-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change InsertQueryBuilder.values() with an empty array into a no…
…-op (#6584) Change InsertQueryBuilder.values() with an empty array into a no-op instead of the current behavior of inserting a row of DEFAULT values. Closes: #3111
- Loading branch information
Showing
4 changed files
with
59 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
|
||
export const DEFAULT_VALUE = "default-value"; | ||
|
||
@Entity() | ||
export class Test { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({default: DEFAULT_VALUE}) | ||
value: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import "reflect-metadata"; | ||
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {expect} from "chai"; | ||
import {InsertValuesMissingError} from "../../../src/error/InsertValuesMissingError"; | ||
import {Test, DEFAULT_VALUE} from "./entity/Test"; | ||
|
||
describe("github issues > #3111 Inserting with query builder attempts to insert a default row when values is empty array", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should not insert with default values on .values([])", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.getRepository(Test); | ||
await repo.createQueryBuilder().insert().values([]).execute(); | ||
const rowsWithDefaultValue = await repo.find({ where: {value: DEFAULT_VALUE}}); | ||
expect(rowsWithDefaultValue).to.have.lengthOf(0); | ||
}))); | ||
|
||
it("should still error on missing .values()", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.getRepository(Test); | ||
await repo.createQueryBuilder().insert().execute().should.be.rejectedWith(InsertValuesMissingError); | ||
}))); | ||
}); |