Skip to content

Commit

Permalink
fix(core): fix aliasing of embeddables in update query (#1650)
Browse files Browse the repository at this point in the history
  • Loading branch information
francisco-sanchez-molina committed Apr 8, 2021
1 parent 2e01587 commit 6cb5f62
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
}

getKnex(): KnexQueryBuilder {
const tableName = this.helper.getTableName(this.entityName) + (this.finalized && [QueryType.SELECT, QueryType.COUNT].includes(this.type) ? ` as ${this.alias}` : '');
const tableName = this.helper.getTableName(this.entityName) + (this.finalized && this.helper.isTableNameAliasRequired(this.type) ? ` as ${this.alias}` : '');
const qb = this.knex(tableName);

if (this.context) {
Expand Down Expand Up @@ -575,7 +575,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
this.type = type;
this._aliasMap[this.alias] = this.entityName;

if (![QueryType.SELECT, QueryType.COUNT].includes(type)) {
if (!this.helper.isTableNameAliasRequired(type)) {
delete this._fields;
}

Expand Down
11 changes: 8 additions & 3 deletions packages/knex/src/query/QueryBuilderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class QueryBuilderHelper {
mapper(field: string, type?: QueryType): string;
mapper(field: string, type?: QueryType, value?: any, alias?: string | null): string;
mapper(field: string, type = QueryType.SELECT, value?: any, alias?: string | null): string | Raw {
const isTableNameAliasRequired = this.isTableNameAliasRequired(type);
const fields = Utils.splitPrimaryKeys(field);

if (fields.length > 1) {
Expand All @@ -43,11 +44,11 @@ export class QueryBuilderHelper {
const noPrefix = prop && prop.persist === false;

if (prop?.fieldNameRaw) {
return this.knex.raw(this.prefix(field, true));
return this.knex.raw(this.prefix(field, isTableNameAliasRequired));
}

if (prop?.customType?.convertToJSValueSQL) {
const prefixed = this.prefix(field, true, true);
const prefixed = this.prefix(field, isTableNameAliasRequired, true);
const valueSQL = prop.customType.convertToJSValueSQL!(prefixed, this.platform);

if (alias === null) {
Expand All @@ -71,7 +72,7 @@ export class QueryBuilderHelper {
return this.knex.raw(ret, value);
}

if (![QueryType.SELECT, QueryType.COUNT].includes(type) || this.isPrefixed(ret) || noPrefix) {
if (!isTableNameAliasRequired || this.isPrefixed(ret) || noPrefix) {
return ret;
}

Expand Down Expand Up @@ -580,4 +581,8 @@ export class QueryBuilderHelper {
return meta ? meta.properties[field] : undefined;
}

isTableNameAliasRequired(type: QueryType): boolean {
return [QueryType.SELECT, QueryType.COUNT].includes(type);
}

}
27 changes: 27 additions & 0 deletions tests/features/embeddables/embedded-entities.postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,33 @@ describe('embedded entities in postgresql', () => {
});
});


test('native update entity', async () => {
const user = new User();
wrap(user).assign({
address1: { street: 'Downing street 10', number: 3, postalCode: '123', city: 'London 1', country: 'UK 1' },
address2: { street: 'Downing street 11', number: 3, city: 'London 2', country: 'UK 2' },
address3: { street: 'Downing street 12', number: 3, postalCode: '789', city: 'London 3', country: 'UK 3' },
address4: { street: 'Downing street 10', number: 3, postalCode: '123', city: 'London 1', country: 'UK 1' },
}, { em: orm.em });

await orm.em.persistAndFlush(user);

await orm.em.nativeUpdate(User, {
address4: {
number: {
$gt: 2,
},
},
}, {
after: 2,
});
orm.em.clear();

const userAfterUpdate = await orm.em.findOne(User, user.id);
expect(userAfterUpdate?.after).toBe(2);
});

test('query by complex custom expressions with JSON operator and casting (GH issue 1261)', async () => {
const user = new User();
user.address1 = new Address1('Test', 10, '12000', 'Prague', 'CZ');
Expand Down

0 comments on commit 6cb5f62

Please sign in to comment.