Skip to content

Commit

Permalink
fix(query-builder): fix aliasing of relations with composite PK
Browse files Browse the repository at this point in the history
Closes #3053
  • Loading branch information
B4nan committed May 29, 2022
1 parent 77d3266 commit 095e241
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/knex/src/query/QueryBuilderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class QueryBuilderHelper {
const prop = this.getProperty(f, a);

if (prop) {
parts.push(...prop.fieldNames.map(f => this.mapper(f, type, value, alias)));
parts.push(...prop.fieldNames.map(f => this.mapper(a !== this.alias ? `${a}.${f}` : f, type, value, alias)));
} else {
parts.push(this.mapper(`${a}.${f}`, type, value, alias));
}
Expand Down
57 changes: 57 additions & 0 deletions tests/issues/GH3053.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Collection, Entity, Enum, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import type { SqliteDriver } from '@mikro-orm/sqlite';

@Entity()
class Book {

@PrimaryKey()
id!: number;

// eslint-disable-next-line @typescript-eslint/no-use-before-define
@OneToMany(() => Author, c => c.book)
children = new Collection<Author>(this);

}

enum AuthorType {
Apple = 'Apple',
Banana = 'Banana',
}

@Entity()
class Author {

@Property({ primary: true })
id!: string;

@Enum({ items: () => AuthorType, primary: true })
type!: AuthorType;

@ManyToOne(() => Book)
book!: Book;

}
let orm: MikroORM<SqliteDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Author, Book],
dbName: ':memory:',
type: 'sqlite',
});
await orm.getSchemaGenerator().createSchema();
});

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

test(`GH issue 3053`, async () => {
const sql = orm.em.qb(Book).where({
children: {
id: '123ABC',
type: AuthorType.Apple,
},
}).getFormattedQuery();
expect(sql).toBe("select `b0`.* from `book` as `b0` left join `author` as `a1` on `b0`.`id` = `a1`.`book_id` where (`a1`.`id`, `a1`.`type`) in (values ('123ABC', 'Apple'))");
});

0 comments on commit 095e241

Please sign in to comment.