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: resolve not returning soft deleted relations with withDeleted find option #8017

Merged
merged 5 commits into from
Aug 10, 2021
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
8 changes: 4 additions & 4 deletions src/find-options/FindOptionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export class FindOptionsUtils {
const metadata = qb.expressionMap.mainAlias!.metadata;

// apply all options from FindOptions
if (options.withDeleted) {
qb.withDeleted();
}

if (options.select) {
qb.select([]);
options.select.forEach(select => {
Expand Down Expand Up @@ -169,10 +173,6 @@ export class FindOptionsUtils {
}
}

if (options.withDeleted) {
qb.withDeleted();
}

if (options.loadRelationIds === true) {
qb.loadAllRelationIds();

Expand Down
3 changes: 3 additions & 0 deletions test/functional/query-builder/soft-delete/entity/Photo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity} from "../../../../../src/decorator/entity/Entity";
import {DeleteDateColumn} from "../../../../../src/decorator/columns/DeleteDateColumn";
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {Counters} from "./Counters";
Expand All @@ -15,4 +16,6 @@ export class Photo {
@Column(type => Counters)
counters: Counters;

@DeleteDateColumn()
deletedAt: Date;
}
7 changes: 6 additions & 1 deletion test/functional/query-builder/soft-delete/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Entity} from "../../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {DeleteDateColumn} from "../../../../../src/decorator/columns/DeleteDateColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {Photo} from "./Photo";
import {JoinColumn,OneToOne} from "../../../../../src";

@Entity()
export class User {
Expand All @@ -15,7 +17,10 @@ export class User {
@Column()
likesCount: number = 0;

@OneToOne(() => Photo)
@JoinColumn()
picture: Photo;

@DeleteDateColumn()
deletedAt: Date;

}
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,44 @@ describe("query builder > soft-delete", () => {

})));

});
it("should find with soft deleted relations", () => Promise.all(connections.map(async connection => {
const photoRepository = connection.getRepository(Photo);
const userRepository = connection.getRepository(User);

const photo1 = new Photo();
photo1.url = "image-1.jpg";

const photo2 = new Photo();
photo2.url = "image-2.jpg";

const user1 = new User();
user1.name = "user-1";
user1.picture = photo1;

const user2 = new User();
user2.name = "user-2";
user2.picture = photo2;

await photoRepository.save(photo1);
await photoRepository.save(photo2);
await userRepository.save(user1);
await userRepository.save(user2);

const users = await userRepository.find({
relations: ["picture"]
});

expect(users[0].picture.deletedAt).to.equal(null);
expect(users[1].picture.deletedAt).to.equal(null);

await photoRepository.softDelete(photo1);

const usersWithSoftDelete = await userRepository.find({
withDeleted: true,
relations: ["picture"]
});

expect(usersWithSoftDelete[0].picture.deletedAt).to.not.equal(null);
expect(usersWithSoftDelete[1].picture.deletedAt).to.equal(null);
})));
});