Skip to content

Commit

Permalink
fix: allow where IsNull for ManyToOne relations
Browse files Browse the repository at this point in the history
Closes: #9980
  • Loading branch information
pixtron committed May 23, 2022
1 parent cf3efec commit f893c05
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4154,6 +4154,16 @@ export class SelectQueryBuilder<Entity>
" " +
parseInt(where[key].value),
)
} else if (where[key].type === "isNull") {
if (relation.isManyToOne) {
andConditions.push(
`${alias}.${propertyPath} IS NULL`,
)
} else {
throw new Error(
`This relation isn't supported by given find operator`,
)
}
}
} else {
// const joinAlias = alias + "_" + relation.propertyName;
Expand Down
20 changes: 20 additions & 0 deletions test/github-issues/8890/entity/Author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Column, Entity, OneToMany, PrimaryColumn } from "../../../../src"
import { Post } from "./Post"

@Entity()
export class Author {
@PrimaryColumn()
id: number

@Column()
firstName: string

@Column()
lastName: string

@Column()
age: number

@OneToMany(() => Post, (post) => post.author)
posts: Post[]
}
24 changes: 24 additions & 0 deletions test/github-issues/8890/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
Column,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
} from "../../../../src"
import { Author } from "./Author"

@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number

@Column()
title: string

@Column()
text: string

@ManyToOne(() => Author, (author) => author.posts, {
nullable: true,
})
author: Author
}
32 changes: 32 additions & 0 deletions test/github-issues/8890/issue-8890-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import "reflect-metadata"
import { EntityManager } from "../../../src"
import { Post } from "./entity/Post"
import { Author } from "./entity/Author"

export async function prepareData(manager: EntityManager) {
const post1 = new Post()
post1.id = 1
post1.title = "Post #1"
post1.text = "About post #1"
await manager.save(post1)

const post2 = new Post()
post2.id = 2
post2.title = "Post #2"
post2.text = "About post #2"
await manager.save(post2)

const post3 = new Post()
post3.id = 3
post3.title = "Post #3"
post3.text = "About post #3"
await manager.save(post3)

const author1 = new Author()
author1.id = 1
author1.firstName = "Timber"
author1.lastName = "Saw"
author1.age = 25
author1.posts = [post1]
await manager.save(author1)
}
51 changes: 51 additions & 0 deletions test/github-issues/8890/issue-8890.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import "reflect-metadata"
import "../../utils/test-setup"
import { DataSource, IsNull } from "../../../src"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { Post } from "./entity/Post"
import { prepareData } from "./issue-8890-utils"

describe("github issues > #8890 it should be possible to query IS NULL on ManyToOne relations", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
__dirname,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("where IsNull", () =>
Promise.all(
connections.map(async (connection) => {
await prepareData(connection.manager)

const posts = await connection
.createQueryBuilder(Post, "post")
.setFindOptions({
where: {
author: IsNull(),
},
})
.orderBy("id", "ASC")
.getMany()
posts.should.be.eql([
{
id: 2,
title: "Post #2",
text: "About post #2",
},
{
id: 3,
title: "Post #3",
text: "About post #3",
},
])
}),
))
})

0 comments on commit f893c05

Please sign in to comment.