Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get correct insert ids for multiple entities inserted (#6668)
* fix: (sqlite) get correct increment primary key for mutiple entities inserted Closes: #2131 * fix: (mysql) get correct increment primary key for mutiple entities inserted Closes: #5973 * test: add test case for fix of #2131 * docs: update note about sqlite lastID
- Loading branch information
Showing
6 changed files
with
75 additions
and
6 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
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,12 @@ | ||
import { PrimaryGeneratedColumn, Entity, Column } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number | null; | ||
|
||
@Column() | ||
title: 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,53 @@ | ||
import "reflect-metadata"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { expect } from "chai"; | ||
import { Post } from "./entity/Post"; | ||
|
||
describe("github issues > #2131 InsertResult return the same primary key", () => { | ||
let connections: Connection[]; | ||
const posts: Post[] = [{ | ||
id: null, | ||
title: "Post 1", | ||
}, { | ||
id: null, | ||
title: "Post 2", | ||
}, { | ||
id: null, | ||
title: "Post 3", | ||
}, { | ||
id: null, | ||
title: "Post 4", | ||
}]; | ||
|
||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
enabledDrivers: ["sqlite", "mysql"], | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should get correct insert ids for multiple entities inserted", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
await connection | ||
.createQueryBuilder() | ||
.insert() | ||
.into(Post) | ||
.values(posts) | ||
.execute(); | ||
|
||
expect(posts[0].id).to.equal(1); | ||
expect(posts[1].id).to.equal(2); | ||
expect(posts[2].id).to.equal(3); | ||
expect(posts[3].id).to.equal(4); | ||
}) | ||
)); | ||
}); |