Skip to content

Commit

Permalink
fix: allow where IsNull for ManyToOne relations (#9031)
Browse files Browse the repository at this point in the history
* fix: allow where IsNull for ManyToOne relations

Closes: #8890

* fix direct FindOperator queries on MTO relation

* fix: allow FindOperator queries on OTO owner column

* chore: prettier formating
  • Loading branch information
pixtron authored Aug 25, 2022
1 parent 88d0ced commit 72728f1
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4154,6 +4154,27 @@ export class SelectQueryBuilder<Entity>
" " +
parseInt(where[key].value),
)
} else {
if (
relation.isManyToOne ||
(relation.isOneToOne &&
relation.isOneToOneOwner)
) {
const aliasPath = `${alias}.${propertyPath}`

andConditions.push(
this.createWhereConditionExpression(
this.getWherePredicateCondition(
aliasPath,
where[key],
),
),
)
} 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 | null
}
10 changes: 10 additions & 0 deletions test/github-issues/8890/entity/Profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

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

@Column()
image: string
}
23 changes: 23 additions & 0 deletions test/github-issues/8890/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Column,
Entity,
OneToOne,
JoinColumn,
PrimaryColumn,
} from "../../../../src"
import { Profile } from "./Profile"

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

@Column()
username: string

@OneToOne(() => Profile, {
nullable: true,
})
@JoinColumn()
profile: Profile | null
}
95 changes: 95 additions & 0 deletions test/github-issues/8890/issue-8890-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import "reflect-metadata"
import { EntityManager } from "../../../src"
import { Post } from "./entity/Post"
import { Author } from "./entity/Author"
import { Profile } from "./entity/Profile"
import { User } from "./entity/User"

export async function prepareDataManyToOne(manager: EntityManager) {
const author1 = new Author()
author1.id = 1
author1.firstName = "Timber"
author1.lastName = "Saw"
author1.age = 25

const author2 = new Author()
author2.id = 2
author2.firstName = "Bob"
author2.lastName = "Miller"
author2.age = 34

const author3 = new Author()
author3.id = 3
author3.firstName = "Max"
author3.lastName = "Newton"
author3.age = 54

await manager.save([author1, author2, author3])

const post1 = new Post()
post1.id = 1
post1.title = "Post #1"
post1.text = "About post #1"
post1.author = author1

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

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

const post4 = new Post()
post4.id = 4
post4.title = "Post #4"
post4.text = "About post #4"
post4.author = author2

const post5 = new Post()
post5.id = 5
post5.title = "Post #5"
post5.text = "About post #5"
post5.author = author3

await manager.save([post1, post2, post3, post4, post5])
}

export async function prepareDataOneToOne(manager: EntityManager) {
const profile1 = new Profile()
profile1.id = 1
profile1.image = "image-1.jpg"

const profile2 = new Profile()
profile2.id = 2
profile2.image = "image-2.jpg"

const profile3 = new Profile()
profile3.id = 3
profile3.image = "image-3.jpg"

await manager.save([profile1, profile2, profile3])

const user1 = new User()
user1.id = 1
user1.username = "user #1"
user1.profile = profile1

const user2 = new User()
user2.id = 2
user2.username = "user #2"
user2.profile = profile2

const user3 = new User()
user3.id = 3
user3.username = "user #3"
user3.profile = profile3

const user4 = new User()
user4.id = 4
user4.username = "user #4"

await manager.save([user1, user2, user3, user4])
}
Loading

0 comments on commit 72728f1

Please sign in to comment.