Skip to content

Commit

Permalink
fix: discard duplicated columns on update (#8724)
Browse files Browse the repository at this point in the history
Closes: #8723
  • Loading branch information
UyumazHakan committed Mar 4, 2022
1 parent f3cfdd2 commit 0fc093d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/query-builder/UpdateQueryBuilder.ts
Expand Up @@ -394,7 +394,8 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
}

columns.forEach(column => {
if (!column.isUpdate) { return; }
if (!column.isUpdate || updatedColumns.includes(column)) { return; }

updatedColumns.push(column);

//
Expand Down
21 changes: 21 additions & 0 deletions 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;
}
14 changes: 14 additions & 0 deletions 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;
}
28 changes: 28 additions & 0 deletions 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 });
})));

});

0 comments on commit 0fc093d

Please sign in to comment.