Repository navigation
Description
Issue Description
Expected Behavior
-
A call to
await getManager().insert(Entity, data);should insert the items in the database
while keeping the data object untouched if the primary keys are explicitly defined. -
When the primary key value is not set, the missing / new key is added automatically in the object.
Actual Behavior
With typeorm being used in our Angular 10 / NodeJS 12 application, I recently started having weird behaviours
upon database objects inserts that require "uuid" primary keys to keep integrity.
The issue must have appeared after an upgrade from [email protected] to [email protected].
Every time you insert new data, the ids are randomly remapped but they all come from the original ones.
console.log(data);
await getManager().insert(Entity, data);
console.log(data);
[
{"id":"8f8aac6c-1984-4714-8765-1f4234b714c1","name":"A", ...},
{"id":"6d1f26fc-ef13-446d-b7bb-a0a12194a42e","name":"B", ...},
{"id":"0526b38e-6acc-4daa-919e-f9d5edafed8a","name":"C", ...}
]
[
{"id":"0526b38e-6acc-4daa-919e-f9d5edafed8a","name":"A", ...},
{"id":"6d1f26fc-ef13-446d-b7bb-a0a12194a42e","name":"B", ...},
{"id":"8f8aac6c-1984-4714-8765-1f4234b714c1","name":"C", ...}
]
Database
8f8aac6c-1984-4714-8765-1f4234b714c1 A ...
6d1f26fc-ef13-446d-b7bb-a0a12194a42e B ...
0526b38e-6acc-4daa-919e-f9d5edafed8a C ...
Steps to Reproduce
Started creating a new test but couldn't reproduce the issue in a test as of now.
npm install -D @types/uuid uuidtest/github-issues/0000/issue-0000.ts
import "reflect-metadata";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils";
import { Connection } from "../../../src/connection/Connection";
import { expect } from "chai";
import { v4 as uuidv4 } from 'uuid';
import { Post } from "./entity/Post";
describe("github issues > #0000 InsertResult return the same primary key", () => {
let connections: Connection[];
const uuid1 = uuidv4();
const uuid2 = uuidv4();
const uuid3 = uuidv4();
const uuid4 = uuidv4();
const posts: Post[] = [{
id: uuid1,
title: "Post 1",
}, {
id: uuid2,
title: "Post 2",
}, {
id: uuid3,
title: "Post 3",
}, {
id: uuid4,
title: "Post 4",
}];
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["sqlite"],
}))
);
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should get original insert ids for multiple entities inserted", () =>
connections.map(async (connection) => {
connection.getRepository(Post).insert(posts);
expect(posts[0].id).to.equal(uuid1);
expect(posts[1].id).to.equal(uuid2);
expect(posts[2].id).to.equal(uuid3);
expect(posts[3].id).to.equal(uuid4);
})
);
});test/github-issues/0000/entity/Post.ts
import { IPost } from "./IPost";
import { PrimaryGeneratedColumn, Entity, Column } from "../../../../src";
@Entity()
export class Post implements IPost {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
title: string;
}test/github-issues/0000/entity/IPost.ts
export interface IPost {
id?: string;
title: string;
}npm run test-fast -- --grep="github issues > #0000"
My Environment
| Dependency | Version |
|---|---|
| Operating System | Linux CentOS (native or Docker) |
| Node.js version | v12.19.0 |
| Typescript version | v4.0.5 |
| TypeORM version | v0.2.29 |
Additional Context
Using TypeORM with the sqlite driver only.
Relevant Database Driver(s)
-
aurora-data-api -
aurora-data-api-pg -
better-sqlite3 -
cockroachdb -
cordova -
expo -
mongodb -
mysql -
nativescript -
oracle -
postgres -
react-native -
sap -
sqlite -
sqlite-abstract -
sqljs -
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time, and I know how to start.
- Yes, I have the time, but I don't know how to start. I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
AdrianDC commentedon Nov 21, 2020
Downgraded to all versions between
[email protected]and[email protected],I can now confirm the issue appeared between
[email protected]and[email protected].In my opinion, the issue is that the
.idprimary keys are being reassigned in an alphanumerical order,the altered object always has the fields sorted, as seen in the issue example too.
I created a draw PR with a test that should match these issues, however I can't reproduce the issue in a test yet...
Feel free to edit the PR later on if progress is made.
AdrianDC commentedon Nov 23, 2020
Awful workaround on
[email protected]until the issue is resolved:ssarj commentedon Aug 30, 2021
I suspect the missing piece of the reproduction was having something that would cause TypeORM to re-select after insert? I've got a reproduction that is using MySQL & an instance of
CreateDateColumndecorator (though, I see I'm using query builder directly). I had a very very quick look and perhaps there's an issue in ReturningResultsEntityUpdator, but I'd need to have a much closer look to get any confidence.If I get some free time I'll look at creating a proper test for it.
github-actions commentedon Sep 18, 2025
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.