-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix mapping of inserted PKs with custom field names from b…
…atch insert Closes #2977
- Loading branch information
Showing
4 changed files
with
124 additions
and
21 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
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,102 @@ | ||
import { MikroORM, Entity, PrimaryKey, ManyToOne } from '@mikro-orm/core'; | ||
|
||
@Entity() | ||
export class Author { | ||
|
||
@PrimaryKey({ name: 'author_id' }) | ||
id!: number; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Book { | ||
|
||
@PrimaryKey({ name: 'book_id' }) | ||
id!: number; | ||
|
||
@ManyToOne(() => Author) | ||
author!: Author; | ||
|
||
} | ||
|
||
test('batch insert and mapping of PKs with custom field name [sqlite]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Author, Book], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
const authors = [new Author(), new Author(), new Author()]; | ||
const books = [new Book(), new Book(), new Book()]; | ||
books.forEach((b, idx) => b.author = authors[idx]); | ||
await orm.em.persist(books).flush(); | ||
expect(authors.map(a => a.id)).toEqual([1, 2, 3]); | ||
expect(books.map(b => b.id)).toEqual([1, 2, 3]); | ||
await orm.close(); | ||
}); | ||
|
||
test('batch insert and mapping of PKs with custom field name [better-sqlite]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Author, Book], | ||
dbName: ':memory:', | ||
type: 'better-sqlite', | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
const authors = [new Author(), new Author(), new Author()]; | ||
const books = [new Book(), new Book(), new Book()]; | ||
books.forEach((b, idx) => b.author = authors[idx]); | ||
await orm.em.persist(books).flush(); | ||
expect(authors.map(a => a.id)).toEqual([1, 2, 3]); | ||
expect(books.map(b => b.id)).toEqual([1, 2, 3]); | ||
await orm.close(); | ||
}); | ||
|
||
test('batch insert and mapping of PKs with custom field name [postgres]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Author, Book], | ||
dbName: 'mikro_orm_test_2977', | ||
type: 'postgresql', | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
const authors = [new Author(), new Author(), new Author()]; | ||
const books = [new Book(), new Book(), new Book()]; | ||
books.forEach((b, idx) => b.author = authors[idx]); | ||
await orm.em.persist(books).flush(); | ||
expect(authors.map(a => a.id)).toEqual([1, 2, 3]); | ||
expect(books.map(b => b.id)).toEqual([1, 2, 3]); | ||
await orm.close(); | ||
}); | ||
|
||
test('batch insert and mapping of PKs with custom field name [mysql]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Author, Book], | ||
dbName: 'mikro_orm_test_2977', | ||
type: 'mysql', | ||
port: 3308, | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
const authors = [new Author(), new Author(), new Author()]; | ||
const books = [new Book(), new Book(), new Book()]; | ||
books.forEach((b, idx) => b.author = authors[idx]); | ||
await orm.em.persist(books).flush(); | ||
expect(authors.map(a => a.id)).toEqual([1, 2, 3]); | ||
expect(books.map(b => b.id)).toEqual([1, 2, 3]); | ||
await orm.close(); | ||
}); | ||
|
||
test('batch insert and mapping of PKs with custom field name [mariadb]', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Author, Book], | ||
dbName: 'mikro_orm_test_2977', | ||
type: 'mariadb', | ||
port: 3309, | ||
}); | ||
await orm.getSchemaGenerator().refreshDatabase(); | ||
const authors = [new Author(), new Author(), new Author()]; | ||
const books = [new Book(), new Book(), new Book()]; | ||
books.forEach((b, idx) => b.author = authors[idx]); | ||
await orm.em.persist(books).flush(); | ||
expect(authors.map(a => a.id)).toEqual([1, 2, 3]); | ||
expect(books.map(b => b.id)).toEqual([1, 2, 3]); | ||
await orm.close(); | ||
}); |