diff --git a/packages/knex/src/query/QueryBuilder.ts b/packages/knex/src/query/QueryBuilder.ts index 91e81b951b43..46c9ecf2f5b3 100644 --- a/packages/knex/src/query/QueryBuilder.ts +++ b/packages/knex/src/query/QueryBuilder.ts @@ -576,6 +576,10 @@ export class QueryBuilder = AnyEntity> { const tableName = this.helper.getTableName(this.entityName) + (this.finalized && this.helper.isTableNameAliasRequired(this.type) ? ` as ${this.alias}` : ''); const qb = this.knex(tableName); + if (this._schema) { + qb.withSchema(this._schema); + } + if (this.context) { qb.transacting(this.context); } diff --git a/tests/features/multiple-schemas/different-schema-from-config.postgres.test.ts b/tests/features/multiple-schemas/different-schema-from-config.postgres.test.ts index 7d674222a561..f1217aad872d 100644 --- a/tests/features/multiple-schemas/different-schema-from-config.postgres.test.ts +++ b/tests/features/multiple-schemas/different-schema-from-config.postgres.test.ts @@ -1,8 +1,8 @@ -import { Entity, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core'; +import { Collection, Entity, ManyToMany, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core'; import type { PostgreSqlDriver } from '@mikro-orm/postgresql'; @Entity() -export class CoreEntity { +export class BookTag { @PrimaryKey() id!: number; @@ -16,6 +16,24 @@ export class CoreEntity { } +@Entity() +export class Book { + + @PrimaryKey() + id!: number; + + @Property() + name: string; + + @ManyToMany(() => BookTag) + tags = new Collection(this); + + constructor(name: string) { + this.name = name; + } + +} + describe('different schema from config', () => { let orm: MikroORM; @@ -23,9 +41,9 @@ describe('different schema from config', () => { beforeAll(async () => { orm = await MikroORM.init({ type: 'postgresql', + entities: [Book, BookTag], dbName: 'mikro_orm_test_gh_2740', schema: 'privateschema', - entities: [CoreEntity], }); await orm.getSchemaGenerator().refreshDatabase(); }); @@ -35,31 +53,43 @@ describe('different schema from config', () => { }); beforeEach(async () => { - await orm.em.nativeDelete(CoreEntity, {}); + await orm.em.nativeDelete('book_tags', {}); + await orm.em.nativeDelete(BookTag, {}); + await orm.em.nativeDelete(Book, {}); orm.em.clear(); }); it('should respect the global schema config', async () => { - const entity = new CoreEntity('n'); + const entity = new Book('n'); + entity.tags.add(new BookTag('t')); await orm.em.persistAndFlush(entity); expect(entity.id).toBeDefined(); orm.em.clear(); - const e = await orm.em.findOne(CoreEntity, entity); + const e = await orm.em.findOne(Book, entity); expect(e).not.toBeNull(); expect(wrap(e).getSchema()).toBe('privateschema'); + e!.tags.set([new BookTag('t2')]); + await orm.em.flush(); }); it('should respect the global schema config (multi insert)', async () => { - await orm.em.fork().persistAndFlush([new CoreEntity('n1'), new CoreEntity('n2'), new CoreEntity('n3')]); + const books = [new Book('n1'), new Book('n2'), new Book('n3')]; + books[0].tags.add(new BookTag('t1')); + books[1].tags.add(new BookTag('t2')); + books[2].tags.add(new BookTag('t3')); + await orm.em.fork().persistAndFlush(books); - const res = await orm.em.find(CoreEntity, {}); + const res = await orm.em.find(Book, {}, { populate: ['tags'] }); expect(res).toHaveLength(3); expect(wrap(res[0]).getSchema()).toBe('privateschema'); expect(wrap(res[1]).getSchema()).toBe('privateschema'); expect(wrap(res[2]).getSchema()).toBe('privateschema'); res.forEach(row => row.name = `name ${row.id}`); + res[0].tags.set([new BookTag('t21')]); + res[1].tags.set([new BookTag('t22')]); + res[2].tags.set([new BookTag('t23')]); await orm.em.flush(); });