diff --git a/packages/knex/src/AbstractSqlDriver.ts b/packages/knex/src/AbstractSqlDriver.ts index 7d38e2c0f327..d25437d15f21 100644 --- a/packages/knex/src/AbstractSqlDriver.ts +++ b/packages/knex/src/AbstractSqlDriver.ts @@ -500,14 +500,11 @@ export abstract class AbstractSqlDriver(meta: EntityMetadata, options: NativeInsertUpdateManyOptions): string { - let tableName = this.platform.quoteIdentifier(meta.collection); + const tableName = this.platform.quoteIdentifier(meta.collection); + const schema = this.getSchemaName(meta, options); - if (options.schema === '*') { - return this.platform.quoteIdentifier(this.config.get('schema')) + '.' + tableName; - } - - if (meta.schema || options.schema) { - tableName = this.platform.quoteIdentifier(options.schema ?? meta.schema!) + '.' + tableName; + if (schema) { + return this.platform.quoteIdentifier(schema) + '.' + tableName; } return tableName; 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 9ee3b6a2b581..7d674222a561 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,4 +1,4 @@ -import { Entity, MikroORM, PrimaryKey, wrap } from '@mikro-orm/core'; +import { Entity, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core'; import type { PostgreSqlDriver } from '@mikro-orm/postgresql'; @Entity() @@ -7,6 +7,13 @@ export class CoreEntity { @PrimaryKey() id!: number; + @Property() + name: string; + + constructor(name: string) { + this.name = name; + } + } describe('different schema from config', () => { @@ -27,8 +34,13 @@ describe('different schema from config', () => { await orm.close(); }); + beforeEach(async () => { + await orm.em.nativeDelete(CoreEntity, {}); + orm.em.clear(); + }); + it('should respect the global schema config', async () => { - const entity = new CoreEntity(); + const entity = new CoreEntity('n'); await orm.em.persistAndFlush(entity); expect(entity.id).toBeDefined(); orm.em.clear(); @@ -38,4 +50,17 @@ describe('different schema from config', () => { expect(wrap(e).getSchema()).toBe('privateschema'); }); + 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 res = await orm.em.find(CoreEntity, {}); + 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}`); + await orm.em.flush(); + }); + });