Skip to content

Commit

Permalink
fix: empty objects being hydrated when eager loading relations that h…
Browse files Browse the repository at this point in the history
…ave a `@VirtualColumn`
  • Loading branch information
alumni authored and Lucian Mocanu committed Dec 30, 2024
1 parent 71addb2 commit 8a7c266
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2882,11 +2882,6 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class RawSqlResultsToEntityTransformer {
entity,
this.driver.prepareHydratedValue(value, column),
)
if (value !== null)
if (value !== null && !column.isVirtualProperty)
// we don't mark it as has data because if we will have all nulls in our object - we don't need such object
hasData = true
})
Expand Down
2 changes: 1 addition & 1 deletion test/github-issues/10431/entity/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Category {

@VirtualColumn({
query: (alias) =>
`SELECT COUNT(*) FROM category WHERE id = ${alias}.id`,
`SELECT COUNT(*) FROM "category" WHERE "id" = ${alias}."id"`,
})
randomVirtualColumn: number

Expand Down
34 changes: 26 additions & 8 deletions test/github-issues/10431/issue-10431.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,51 @@ import { DataSource } from "../../../src"
import { expect } from "chai"

import { Category, Product } from "./entity"
import { DriverUtils } from "../../../src/driver/DriverUtils"

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,
})),
)

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) => {
// By default, MySQL uses backticks instead of quotes for identifiers
if (DriverUtils.isMySQLFamily(connection.driver)) {
const randomVirtualColumnMetadata = connection
.getMetadata(Category)
.columns.find(
(columnMetadata) =>
columnMetadata.propertyName ===
"randomVirtualColumn",
)!

randomVirtualColumnMetadata.query = (alias) =>
`SELECT COUNT(*) FROM \`category\` WHERE \`id\` = ${alias}.\`id\``
}

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([])
}),
Expand Down

0 comments on commit 8a7c266

Please sign in to comment.