Skip to content

Commit

Permalink
fix(core): respect specified schema when populating (select-in) (#2676)
Browse files Browse the repository at this point in the history
Co-authored-by: Adlan Arif Zakaria <[email protected]>
  • Loading branch information
adlanarifzr and Adlan Arif Zakaria authored Jan 27, 2022
1 parent 4393337 commit 21a1be0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/core/src/entity/EntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type EntityLoaderOptions<T, P extends string = never> = {
filters?: Dictionary<boolean | Dictionary> | string[] | boolean;
strategy?: LoadStrategy;
lockMode?: Exclude<LockMode, LockMode.OPTIMISTIC>;
schema?: string;
};

export class EntityLoader {
Expand Down Expand Up @@ -247,14 +248,15 @@ export class EntityLoader {
const ids = Utils.unique(children.map(e => Utils.getPrimaryKeyValues(e, e.__meta!.primaryKeys, true)));
const where = this.mergePrimaryCondition<T>(ids, fk, options, meta, this.metadata, this.driver.getPlatform());
const fields = this.buildFields(options.fields, prop);
const { refresh, filters, convertCustomTypes, lockMode, strategy, populateWhere } = options;
const { refresh, filters, convertCustomTypes, lockMode, strategy, populateWhere, schema } = options;

return this.em.find(prop.type, where, {
refresh, filters, convertCustomTypes, lockMode, populateWhere,
orderBy: [...Utils.asArray(options.orderBy), ...Utils.asArray(prop.orderBy), { [fk]: QueryOrder.ASC }] as QueryOrderMap<T>[],
populate: populate.children as never ?? populate.all,
strategy,
fields,
schema,
});
}

Expand Down
73 changes: 73 additions & 0 deletions tests/issues/GH2675.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Entity, LoadStrategy, ManyToOne, MikroORM, PrimaryKey, wrap } from '@mikro-orm/core';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';

@Entity()
export class A {

@PrimaryKey()
id!: number;

}

@Entity()
export class B {

@PrimaryKey()
id!: number;

@ManyToOne({ entity: () => A, onDelete: 'cascade' })
a!: A;

}

describe('GH issue 2675', () => {

let orm: MikroORM<PostgreSqlDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [A, B],
dbName: 'mikro_orm_test_gh_2675',
type: 'postgresql',
});
await orm.getSchemaGenerator().ensureDatabase();

// Create schema dynamically
await orm.getSchemaGenerator().execute(`drop schema if exists myschema cascade`);
await orm.getSchemaGenerator().execute(`create schema if not exists myschema`);

// Initialize DB for dynamic schema entity
await orm.getSchemaGenerator().execute(`create table "myschema"."a" ("id" serial primary key);`);
await orm.getSchemaGenerator().execute(`create table "myschema"."b" ("id" serial primary key, "a_id" int not null);`);
await orm.getSchemaGenerator().execute(`alter table "myschema"."b" add constraint "b_a_id_foreign" foreign key ("a_id") references "myschema"."a" ("id") on update cascade on delete cascade;`);
});

afterAll(() => orm.close(true));

test('should query with specified schema without throwing sql exception', async () => {
// Create sample data
const a = orm.em.create(A, {}, {
schema: 'myschema',
});
expect(wrap(a).getSchema()).toBe('myschema');
orm.em.persist(a);

const b = orm.em.create(B, { a }, {
schema: 'myschema',
});
expect(wrap(b).getSchema()).toBe('myschema');
orm.em.persist(b);

await orm.em.flush();

const r = await orm.em.fork().findOne(B, b.id, {
populate: ['a'],
strategy: LoadStrategy.SELECT_IN,
schema: 'myschema',
}).catch(() => {
// Undefined if exception thrown
});

expect(r?.a?.id).not.toEqual(undefined);
});
});

0 comments on commit 21a1be0

Please sign in to comment.