Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: discard duplicated columns on update #8724

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 });
})));

});