From 22e74700ee64eada056cdfc18e46f5c62416216c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Fri, 17 Nov 2023 10:36:27 +0100 Subject: [PATCH] fix(schema): respect explicit schema in FKs to STI entities Closes #4933 --- packages/knex/src/schema/DatabaseTable.ts | 2 +- .../features/schema-generator/GH4919.test.ts | 2 +- .../features/schema-generator/GH4933.test.ts | 49 +++++++++++++++++++ .../__snapshots__/GH4919.test.ts.snap | 4 +- .../__snapshots__/GH4933.test.ts.snap | 18 +++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 tests/features/schema-generator/GH4933.test.ts create mode 100644 tests/features/schema-generator/__snapshots__/GH4933.test.ts.snap diff --git a/packages/knex/src/schema/DatabaseTable.ts b/packages/knex/src/schema/DatabaseTable.ts index d91162d98fa3..9ae4f2c6c4dd 100644 --- a/packages/knex/src/schema/DatabaseTable.ts +++ b/packages/knex/src/schema/DatabaseTable.ts @@ -132,7 +132,7 @@ export class DatabaseTable { if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) { const constraintName = this.getIndexName(true, prop.fieldNames, 'foreign'); - let schema = prop.targetMeta!.schema === '*' ? this.schema : (prop.targetMeta!.schema ?? config.get('schema', this.platform.getDefaultSchemaName())); + let schema = prop.targetMeta!.root.schema === '*' ? this.schema : (prop.targetMeta!.root.schema ?? config.get('schema', this.platform.getDefaultSchemaName())); if (prop.referencedTableName.includes('.')) { schema = undefined; diff --git a/tests/features/schema-generator/GH4919.test.ts b/tests/features/schema-generator/GH4919.test.ts index f133c4e34fe2..dd3a95bf0cb5 100644 --- a/tests/features/schema-generator/GH4919.test.ts +++ b/tests/features/schema-generator/GH4919.test.ts @@ -42,7 +42,7 @@ beforeAll(async () => { afterAll(() => orm.close(true)); -test('4782', async () => { +test('GH #4919', async () => { const testMigration = async (e1: any, e2: any, snap: string) => { if (e2) { orm.getMetadata().reset(e1.name); diff --git a/tests/features/schema-generator/GH4933.test.ts b/tests/features/schema-generator/GH4933.test.ts new file mode 100644 index 000000000000..d8ac9055f100 --- /dev/null +++ b/tests/features/schema-generator/GH4933.test.ts @@ -0,0 +1,49 @@ +import { MikroORM } from '@mikro-orm/postgresql'; +import { Entity, PrimaryKey, ManyToOne, Enum, type Rel } from '@mikro-orm/core'; + +@Entity({ tableName: 'users', schema: 'example', discriminatorColumn: 'type', abstract: true }) +class Base { + + @PrimaryKey() + id!: string; + + @Enum({ items: ['one', 'two'] }) + type!: 'one' | 'two'; + +} + +@Entity({ discriminatorValue: 'one' }) +class One extends Base {} + +@Entity({ discriminatorValue: 'two' }) +class Two extends Base {} + +@Entity({ tableName: 'relations' }) +class Relation { + + @PrimaryKey() + id!: string; + + @ManyToOne(() => Two, { deleteRule: 'set null' }) + appliedBy?: Rel; + +} + +let orm: MikroORM; + +beforeAll(async () => { + orm = await MikroORM.init({ + entities: [One, Two, Base, Relation], + dbName: '4933', + }); + await orm.schema.ensureDatabase(); + await orm.schema.dropSchema(); +}); + +afterAll(() => orm.close(true)); + +test('GH #4933', async () => { + const sql = await orm.schema.getCreateSchemaSQL(); + expect(sql).toMatchSnapshot(); + await orm.schema.execute(sql); +}); diff --git a/tests/features/schema-generator/__snapshots__/GH4919.test.ts.snap b/tests/features/schema-generator/__snapshots__/GH4919.test.ts.snap index 7c1413f0e6d4..5d14d82a71b3 100644 --- a/tests/features/schema-generator/__snapshots__/GH4919.test.ts.snap +++ b/tests/features/schema-generator/__snapshots__/GH4919.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`4782: 0. create schema 1`] = ` +exports[`GH #4919: 0. create schema 1`] = ` { "down": "drop table if exists "user" cascade; @@ -11,7 +11,7 @@ exports[`4782: 0. create schema 1`] = ` } `; -exports[`4782: 1. rename column 1`] = ` +exports[`GH #4919: 1. rename column 1`] = ` { "down": "alter table "user" drop column "canceled_at"; alter table "user" drop column "arrived_at"; diff --git a/tests/features/schema-generator/__snapshots__/GH4933.test.ts.snap b/tests/features/schema-generator/__snapshots__/GH4933.test.ts.snap new file mode 100644 index 000000000000..e3f34f501f75 --- /dev/null +++ b/tests/features/schema-generator/__snapshots__/GH4933.test.ts.snap @@ -0,0 +1,18 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GH #4933 1`] = ` +"set names 'utf8'; +set session_replication_role = 'replica'; + +create schema if not exists "example"; + +create table "example"."users" ("id" varchar(255) not null, "type" text check ("type" in ('one', 'two')) not null, constraint "users_pkey" primary key ("id")); +create index "users_type_index" on "example"."users" ("type"); + +create table "relations" ("id" varchar(255) not null, "applied_by_id" varchar(255) not null, constraint "relations_pkey" primary key ("id")); + +alter table "relations" add constraint "relations_applied_by_id_foreign" foreign key ("applied_by_id") references "example"."users" ("id") on update cascade on delete set null; + +set session_replication_role = 'origin'; +" +`;