Skip to content

Commit

Permalink
fix(core): ignore virtual properties in partial loading hint
Browse files Browse the repository at this point in the history
Closes #5261
  • Loading branch information
B4nan committed Feb 21, 2024
1 parent 095b979 commit d327db5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,10 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
return;
}

if (prop.persist === false && !prop.embedded && !prop.formula) {
return;
}

ret.push(prop.name);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,10 @@ export class QueryBuilder<T extends object = AnyEntity> {
return;
}

if (prop?.persist === false && !prop.embedded && !prop.formula && type === 'where') {
return;
}

if (prop?.embedded) {
const name = prop.embeddedPath?.join('.') ?? prop.fieldNames[0];
const aliased = this._aliases[a] ? `${a}.${name}` : name;
Expand Down
7 changes: 6 additions & 1 deletion tests/issues/GH3897.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class Author {
@Property()
termsAccepted: boolean = false;

@Property({ persist: false })
foo = '123';

}

let orm: MikroORM;
Expand All @@ -33,7 +36,9 @@ test('GH issue 3897', async () => {
author2.termsAccepted = true;
await orm.em.flush();

const r1 = await orm.em.fork().find(Author, {});
const r1 = await orm.em.fork().find(Author, {}, {
fields: ['foo', 'termsAccepted'],
});
expect(r1[0].termsAccepted).toBe(true);
expect(r1[1].termsAccepted).toBe(true);
});
9 changes: 7 additions & 2 deletions tests/issues/GH535.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ describe('GH issue 535', () => {

orm.em.clear();

const fetchedA = await orm.em.findOneOrFail(A, { id: a.id }, { populate: ['b'] });
expect(fetchedA.calcProp).toBe('foo');
const fetchedA1 = await orm.em.findOneOrFail(A, { id: a.id }, { fields: ['b', 'calcProp'], populate: ['b'] });
expect(fetchedA1.calcProp).toBe('foo');
expect(wrap(fetchedA1).toObject()).toEqual({ id: 1, b: { id: 1 }, calcProp: 'foo' });

const fetchedA2 = await orm.em.fork().qb(A).where({ id: a.id }).select(['id', 'calcProp']).leftJoinAndSelect('b', 'b');
expect(fetchedA2[0].calcProp).toBe('foo');
expect(wrap(fetchedA2[0]).toObject()).toEqual({ id: 1, b: { id: 1, a: 1, prop: 'foo' }, calcProp: 'foo' });
});
});

0 comments on commit d327db5

Please sign in to comment.