-
-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): respect specified schema when populating (select-in) (#2676)
Co-authored-by: Adlan Arif Zakaria <[email protected]>
- Loading branch information
1 parent
4393337
commit 21a1be0
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |