-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: empty objects being hydrated when eager loading relations that h…
…ave a `@VirtualColumn` (#10432) * test: add scenario for #10431 * fix: empty objects being hydrated by unselected virtual properties
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToMany, | ||
PrimaryGeneratedColumn, | ||
VirtualColumn, | ||
} from "../../../../src" | ||
import { Product } from "./Product" | ||
|
||
@Entity() | ||
export class Category { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@VirtualColumn({ | ||
query: (alias) => | ||
`SELECT COUNT(*) FROM category WHERE id = ${alias}.id`, | ||
}) | ||
randomVirtualColumn: number | ||
|
||
@ManyToMany(() => Product, (product: Product) => product.categories) | ||
products?: Product[] | ||
|
||
@Column("varchar") | ||
name: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
Column, | ||
Entity, | ||
JoinTable, | ||
ManyToMany, | ||
PrimaryGeneratedColumn, | ||
} from "../../../../src" | ||
import { Category } from "./Category" | ||
|
||
@Entity() | ||
export class Product { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column("varchar") | ||
name: string | ||
|
||
@ManyToMany(() => Category, (category: Category) => category.products, { | ||
eager: true, | ||
cascade: ["insert", "update", "remove"], | ||
orphanedRowAction: "delete", | ||
}) | ||
@JoinTable() | ||
categories: Category[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./Category" | ||
export * from "./Product" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src" | ||
import { expect } from "chai" | ||
|
||
import { Category, Product } from "./entity" | ||
|
||
describe("github issues > #10431 When requesting nested relations on foreign key primary entities, relation becomes empty entity rather than null", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [Category, Product], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("should return [] when requested nested relations are empty on ManyToMany relation with @VirtualColumn definitions", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const productRepo = connection.getRepository(Product) | ||
const testProduct = new Product() | ||
testProduct.name = "foo" | ||
await productRepo.save(testProduct) | ||
const foundProduct = await productRepo.findOne({ | ||
where: { | ||
id: testProduct.id, | ||
}, | ||
relations: { categories: true }, | ||
}) | ||
expect(foundProduct?.name).eq("foo") | ||
expect(foundProduct?.categories).eql([]) | ||
}), | ||
)) | ||
}) |