Skip to content

Commit

Permalink
fix(core): do not infer populate: ['*'] from fields: ['*']
Browse files Browse the repository at this point in the history
Closes #5139
  • Loading branch information
B4nan committed Jan 17, 2024
1 parent 50f93c1 commit f658376
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/EntityManager.ts
Expand Up @@ -2028,11 +2028,11 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
// we need to prune the `populate` hint from to-one relations, as partially loading them does not require their population, we want just the FK
const pruneToOneRelations = (meta: EntityMetadata, fields: string[]): string[] => {
return fields.filter(field => {
if (!field.includes('.')) {
if (field === '*') {
return true;
}
if (field === '*') {
return false;
}

if (!field.includes('.')) {
return ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(meta.properties[field].kind);
}

Expand All @@ -2041,7 +2041,7 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {

/* istanbul ignore next */
if (key === '*') {
return true;
return false;
}

const prop = meta.properties[key];
Expand Down
55 changes: 55 additions & 0 deletions tests/features/partial-loading/GH5139.test.ts
@@ -0,0 +1,55 @@
import {
Entity,
Ref,
ManyToOne,
MikroORM,
PrimaryKey, OneToMany, Collection,
} from '@mikro-orm/sqlite';

@Entity()
class City {

@PrimaryKey()
id!: number;

@OneToMany(() => School, s => s.city)
schools = new Collection<School>(this);

}

@Entity()
class School {

@PrimaryKey()
id!: number;

@ManyToOne(() => City, { ref: true })
city!: Ref<City>;

}


let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [City],
});
await orm.schema.createSchema();

await orm.em.insert(City, { id: 1 });
await orm.em.insertMany(School, [
{ id: 1, city: 1 },
{ id: 2, city: 1 },
]);
});

afterAll(async () => {
await orm.close(true);
});

test(`GH issue 5139`, async () => {
const city = await orm.em.find(City, { id: 1 }, { fields: ['*'] });
expect(city[0].schools.isInitialized()).toBe(false);
});

0 comments on commit f658376

Please sign in to comment.