Skip to content

Commit

Permalink
fix(query-builder): fix aliasing of joined embedded properties
Browse files Browse the repository at this point in the history
Closes #4711
  • Loading branch information
B4nan committed Sep 20, 2023
1 parent 3624cb7 commit 24c4ece
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ export class QueryBuilder<T extends object = AnyEntity> {
}

if (prop?.embedded) {
const fieldName = this.helper.mapper(prop.fieldNames[0], this.type) as string;
const name = this._aliases[a] ? `${a}.${prop.fieldNames[0]}` : prop.fieldNames[0];
const fieldName = this.helper.mapper(name, this.type) as string;
ret.push(fieldName);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ exports[`embedded entities in postgresql schema: embeddables 1 1`] = `
"create table "user" ("id" serial primary key, "email" varchar(255) not null, "address1_street" varchar(255) null, "address1_number" int not null, "address1_rank" real null, "address1_postal_code" varchar(255) null, "address1_city" varchar(255) null, "address1_country" varchar(255) null, "addr_street" varchar(255) null, "addr_postal_code" varchar(255) null, "addr_city" varchar(255) null, "addr_country" varchar(255) null, "street" varchar(255) null, "number" int not null, "rank" real null, "postal_code" varchar(255) null, "city" varchar(255) null, "country" varchar(255) null, "address4" jsonb not null, "addresses" jsonb not null, "after" int null);
alter table "user" add constraint "user_email_unique" unique ("email");
create table "foo" ("id" serial primary key, "user_id" int not null);
alter table "foo" add constraint "foo_user_id_foreign" foreign key ("user_id") references "user" ("id") on update cascade;
"
`;

exports[`embedded entities in postgresql schema: embeddables 2 1`] = `""`;

exports[`embedded entities in postgresql schema: embeddables 3 1`] = `
"drop table if exists "user" cascade;
"drop table if exists "foo" cascade;
drop table if exists "user" cascade;
"
`;
48 changes: 46 additions & 2 deletions tests/features/embeddables/embedded-entities.postgres.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Embeddable, Embedded, Entity, expr, MikroORM, PrimaryKey, Property, ReferenceType, t } from '@mikro-orm/core';
import {
Embeddable,
Embedded,
Entity,
expr,
ManyToOne,
MikroORM,
PrimaryKey,
Property,
ReferenceType, Rel,
t,
} from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { mockLogger } from '../../helpers';

Expand Down Expand Up @@ -57,6 +68,17 @@ class Address2 {

}

@Entity()
class Foo {

@PrimaryKey()
id!: number;

@ManyToOne(() => User)
user!: Rel<User>;

}

@Entity()
class User {

Expand Down Expand Up @@ -92,7 +114,7 @@ describe('embedded entities in postgresql', () => {

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
entities: [User, Foo],
dbName: 'mikro_orm_test_embeddables',
driver: PostgreSqlDriver,
});
Expand Down Expand Up @@ -389,6 +411,28 @@ describe('embedded entities in postgresql', () => {
expect(userAfterUpdate?.after).toBe(2);
});

test('GH #4711', async () => {
const user = new User();
user.email = `test-${Math.random()}`;
user.address1 = new Address1('Test 1', 10, '12000', 'Prague', 'CZ');
user.address3 = new Address1('Test 3', 10, '12000', 'Prague', 'CZ');
user.address4 = new Address1('Test 4', 10, '12000', 'Prague', 'CZ');
const foo = new Foo();
foo.user = user;
await orm.em.fork().persistAndFlush(foo);

const query = orm.em.qb(Foo, 'f')
.leftJoin('f.user', 'u')
.select(['f.*', 'u.street']);
expect(query.getQuery()).toBe('select "f".*, "u"."street" from "foo" as "f" left join "user" as "u" on "f"."user_id" = "u"."id"');
await expect(query).resolves.toEqual([
{
id: 1,
user: { id: 1 },
},
]);
});

test('query by complex custom expressions with JSON operator and casting (GH issue 1261)', async () => {
const user = new User();
user.email = `test-${Math.random()}`;
Expand Down

0 comments on commit 24c4ece

Please sign in to comment.