mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fix: empty objects being hydrated when eager loading relations that have a @VirtualColumn (#10432)
* test: add scenario for #10431 * fix: empty objects being hydrated by unselected virtual properties
This commit is contained in:
parent
2dc9624d00
commit
b53e410e5a
@ -2882,6 +2882,11 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
|
||||
})
|
||||
})
|
||||
} else {
|
||||
if (column.isVirtualProperty) {
|
||||
// Do not add unselected virtual properties to final select
|
||||
return
|
||||
}
|
||||
|
||||
finalSelects.push({
|
||||
selection: selectionPath,
|
||||
aliasName: DriverUtils.buildAlias(
|
||||
|
||||
26
test/github-issues/10431/entity/Category.ts
Normal file
26
test/github-issues/10431/entity/Category.ts
Normal file
@ -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
|
||||
}
|
||||
25
test/github-issues/10431/entity/Product.ts
Normal file
25
test/github-issues/10431/entity/Product.ts
Normal file
@ -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[]
|
||||
}
|
||||
2
test/github-issues/10431/entity/index.ts
Normal file
2
test/github-issues/10431/entity/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./Category"
|
||||
export * from "./Product"
|
||||
42
test/github-issues/10431/issue-10431.ts
Normal file
42
test/github-issues/10431/issue-10431.ts
Normal file
@ -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([])
|
||||
}),
|
||||
))
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user