diff --git a/src/query-builder/UpdateQueryBuilder.ts b/src/query-builder/UpdateQueryBuilder.ts index 45d5883fe5..7eb8978379 100644 --- a/src/query-builder/UpdateQueryBuilder.ts +++ b/src/query-builder/UpdateQueryBuilder.ts @@ -394,7 +394,8 @@ export class UpdateQueryBuilder extends QueryBuilder implements } columns.forEach(column => { - if (!column.isUpdate) { return; } + if (!column.isUpdate || updatedColumns.includes(column)) { return; } + updatedColumns.push(column); // diff --git a/test/github-issues/8723/entity/Photo.ts b/test/github-issues/8723/entity/Photo.ts new file mode 100644 index 0000000000..96469bb793 --- /dev/null +++ b/test/github-issues/8723/entity/Photo.ts @@ -0,0 +1,21 @@ +import { + Column, + Entity, + OneToOne, + JoinColumn, + PrimaryColumn, +} from "../../../../src"; +import { User } from "./User"; + +@Entity() +export class Photo { + @PrimaryColumn({ type: "int", nullable: false }) + id: number; + + @OneToOne(() => User, { nullable: true }) + @JoinColumn({ name: "user_id" }) + public user?: User; + + @Column({ name: "user_id", nullable: true }) + public userId?: number; +} diff --git a/test/github-issues/8723/entity/User.ts b/test/github-issues/8723/entity/User.ts new file mode 100644 index 0000000000..801bf761f2 --- /dev/null +++ b/test/github-issues/8723/entity/User.ts @@ -0,0 +1,14 @@ +import { + Column, + Entity, + PrimaryColumn, +} from "../../../../src"; + +@Entity() +export class User { + @PrimaryColumn({ type: "int", nullable: false }) + id: number; + + @Column() + name: string; +} diff --git a/test/github-issues/8723/issue-8723.ts b/test/github-issues/8723/issue-8723.ts new file mode 100644 index 0000000000..282effd69c --- /dev/null +++ b/test/github-issues/8723/issue-8723.ts @@ -0,0 +1,28 @@ +import "reflect-metadata"; +import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; +import { Connection } from "../../../src/connection/Connection"; +import { User } from "./entity/User"; +import { Photo } from "./entity/Photo"; + +describe("github issues > #8723 Fail on Update when reference exists together with FK: multiple assignments to same column ", () => { + + 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 able to update when both reference and the id exist in the update object", () => Promise.all(connections.map(async connection => { + const photoRepository = connection.getRepository(Photo); + const userRepository = connection.getRepository(User); + + const user = await userRepository.save({ id: 1, name: "Test" }); + const photo = await photoRepository.save({ id: 1 }); + + await photoRepository.update({ id: photo.id }, { user, userId: user.id }); + }))); + +}); \ No newline at end of file