Skip to content

Commit

Permalink
fix(schema): respect explicit schema in FKs to STI entities
Browse files Browse the repository at this point in the history
Closes #4933
  • Loading branch information
B4nan committed Nov 17, 2023
1 parent dd565a7 commit 22e7470
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/knex/src/schema/DatabaseTable.ts
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/features/schema-generator/GH4919.test.ts
Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions 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<Two>;

}

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);
});
@@ -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;
Expand All @@ -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";
Expand Down
18 changes: 18 additions & 0 deletions 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';
"
`;

0 comments on commit 22e7470

Please sign in to comment.