Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): respect explicit schema in FKs to STI entities
Closes #4933
- Loading branch information
Showing
5 changed files
with
71 additions
and
4 deletions.
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
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,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); | ||
}); |
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
18 changes: 18 additions & 0 deletions
18
tests/features/schema-generator/__snapshots__/GH4933.test.ts.snap
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,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'; | ||
" | ||
`; |