Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): prefer custom pivot entity for inference of FK names
Closes #3626
- Loading branch information
Showing
3 changed files
with
114 additions
and
3 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
17 changes: 17 additions & 0 deletions
17
tests/features/composite-keys/__snapshots__/custom-pivot-entity-with-sti.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,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`schema 1`] = ` | ||
"pragma foreign_keys = off; | ||
create table \`task\` (\`id\` integer not null primary key autoincrement); | ||
create table \`user\` (\`id\` integer not null primary key autoincrement, \`role\` text not null default 'CREATOR'); | ||
create index \`user_role_index\` on \`user\` (\`role\`); | ||
create table \`creators_on_tasks\` (\`creator_id\` integer not null, \`task_id\` integer not null, constraint \`creators_on_tasks_creator_id_foreign\` foreign key(\`creator_id\`) references \`user\`(\`id\`) on update cascade, constraint \`creators_on_tasks_task_id_foreign\` foreign key(\`task_id\`) references \`task\`(\`id\`) on update cascade, primary key (\`creator_id\`, \`task_id\`)); | ||
create index \`creators_on_tasks_creator_id_index\` on \`creators_on_tasks\` (\`creator_id\`); | ||
create index \`creators_on_tasks_task_id_index\` on \`creators_on_tasks\` (\`task_id\`); | ||
pragma foreign_keys = on; | ||
" | ||
`; |
86 changes: 86 additions & 0 deletions
86
tests/features/composite-keys/custom-pivot-entity-with-sti.test.ts
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,86 @@ | ||
import { Collection, Entity, ManyToMany, ManyToOne, MikroORM, PrimaryKey, Property } from '@mikro-orm/core'; | ||
|
||
@Entity({ | ||
discriminatorColumn: 'role', | ||
discriminatorMap: { | ||
CREATOR: 'Creator', | ||
}, | ||
}) | ||
class User { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
role: 'CREATOR' = 'CREATOR' as const; | ||
|
||
} | ||
|
||
@Entity() | ||
class Creator extends User { | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@ManyToMany({ entity: () => Task, pivotEntity: () => CreatorsOnTasks }) | ||
tasks = new Collection<Task>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class Task { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToMany(() => Creator, c => c.tasks) | ||
creators = new Collection<Creator>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class CreatorsOnTasks { | ||
|
||
@ManyToOne({ primary: true, entity: () => Creator }) | ||
creator!: Creator; | ||
|
||
@ManyToOne({ primary: true, entity: () => Task }) | ||
task!: Task; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [User, Creator, CreatorsOnTasks, Task], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => await orm.close(true)); | ||
|
||
beforeEach(() => orm.schema.clearDatabase()); | ||
|
||
test('schema', async () => { | ||
const sql = await orm.schema.getCreateSchemaSQL(); | ||
expect(sql).toMatchSnapshot(); | ||
}); | ||
|
||
async function createEntities() { | ||
const task = new Task(); | ||
const creator = new Creator(); | ||
task.creators.add(creator); | ||
await orm.em.fork().persistAndFlush(task); | ||
|
||
return { task }; | ||
} | ||
|
||
test('should insert', async () => { | ||
await expect(createEntities()).resolves.not.toThrow(); | ||
}); | ||
|
||
test('should not findOne and populate m:n relation', async () => { | ||
const { task } = await createEntities(); | ||
await expect(orm.em.findOne(Task, { id: task.id }, { populate: ['creators'] })).resolves.not.toThrow(); | ||
}); |