Skip to content

Commit

Permalink
fix(query-builder): make sure we use the right alias in complex $and …
Browse files Browse the repository at this point in the history
…queries

Closes #786
  • Loading branch information
B4nan committed Aug 27, 2020
1 parent 06a5490 commit 522787e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/entity/EntityFactory.ts
@@ -1,8 +1,8 @@
import { ObjectBindingPattern, Utils } from '../utils';
import { Utils } from '../utils';
import { Dictionary, EntityData, EntityMetadata, EntityName, EntityProperty, New, Populate, Primary } from '../typings';
import { UnitOfWork } from '../unit-of-work';
import { ReferenceType } from './enums';
import { EntityManager, EventType, wrap } from '..';
import { EntityManager, EventType } from '..';

export const SCALAR_TYPES = ['string', 'number', 'boolean', 'Date', 'Buffer', 'RegExp'];

Expand Down Expand Up @@ -138,7 +138,7 @@ export class EntityFactory {
return this.createReference(meta.properties[k].type, data[k]);
}

if (!(k in data) || k as unknown === ObjectBindingPattern) {
if (!meta.properties[k]) {
return data;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/query/QueryBuilder.ts
Expand Up @@ -251,7 +251,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
}

getAliasForJoinPath(path: string): string | undefined {
if (path === this.entityName) {
if (!path || path === this.entityName) {
return this.alias;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/QueryBuilder.test.ts
Expand Up @@ -1410,6 +1410,34 @@ describe('QueryBuilder', () => {
expect(qb.getQuery()).toEqual('select `e0`.* from `publisher2` as `e0` order by length(name) desc, `e0`.`type` asc');
});

test('GH issue 786', async () => {
const qb1 = orm.em.createQueryBuilder(Book2);
qb1.select('*').where({
$and: [
{ uuid: { $ne: '...' }, createdAt: { $gt: '2020-08-26T20:01:48.863Z' } },
{ tags: { name: { $in: ['tag1'] } } },
],
});
expect(qb1.getQuery()).toEqual('select `e0`.*, `e0`.price * 1.19 as `price_taxed` ' +
'from `book2` as `e0` ' +
'left join `book2_tags` as `e2` on `e0`.`uuid_pk` = `e2`.`book2_uuid_pk` ' +
'left join `book_tag2` as `e1` on `e2`.`book_tag2_id` = `e1`.`id` ' +
'where `e0`.`uuid_pk` != ? and `e0`.`created_at` > ? and `e1`.`name` in (?)');

const qb2 = orm.em.createQueryBuilder(Book2);
qb2.select('*').where({
$and: [
{ tags: { name: { $in: ['tag1'] } } },
{ uuid: { $ne: '...' }, createdAt: { $gt: '2020-08-26T20:01:48.863Z' } },
],
});
expect(qb2.getQuery()).toEqual('select `e0`.*, `e0`.price * 1.19 as `price_taxed` ' +
'from `book2` as `e0` ' +
'left join `book2_tags` as `e2` on `e0`.`uuid_pk` = `e2`.`book2_uuid_pk` ' +
'left join `book_tag2` as `e1` on `e2`.`book_tag2_id` = `e1`.`id` ' +
'where `e1`.`name` in (?) and `e0`.`uuid_pk` != ? and `e0`.`created_at` > ?');
});

test('pg array operators', async () => {
const pg = await MikroORM.init<PostgreSqlDriver>({
entities: [Author2, Address2, Book2, BookTag2, Publisher2, Test2, BaseEntity2, Configuration2],
Expand Down

0 comments on commit 522787e

Please sign in to comment.