-
-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(postgres): respect known schema when loading wild card entity rel…
…ations Closes #2909
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Entity, ManyToOne, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core'; | ||
|
||
@Entity({ schema: '*' }) | ||
export class Author { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
name!: string; | ||
|
||
} | ||
|
||
@Entity({ schema: '*' }) | ||
export class Book { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@ManyToOne({ entity: () => Author, onDelete: 'cascade' }) | ||
author!: Author; | ||
|
||
} | ||
|
||
describe('GH issue 2909', () => { | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Author, Book], | ||
dbName: 'mikro_orm_test_2909', | ||
type: 'postgresql', | ||
}); | ||
|
||
await orm.getSchemaGenerator().refreshDatabase(); | ||
await orm.getSchemaGenerator().updateSchema({ schema: 'test' }); | ||
await orm.getSchemaGenerator().clearDatabase({ schema: 'test' }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('populate already loaded entities', async () => { | ||
const b = new Book(); | ||
b.name = 'b'; | ||
const author = new Author(); | ||
author.name = 'a'; | ||
b.author = author; | ||
wrap(b).setSchema('test'); | ||
wrap(b.author).setSchema('test'); | ||
await orm.em.fork().persistAndFlush(b); | ||
const books = await orm.em.getRepository(Book).findAll({ schema: 'test' }); | ||
|
||
expect(wrap(books[0]).getSchema()).toBe('test'); | ||
expect(wrap(books[0].author).getSchema()).toBe('test'); | ||
expect(wrap(books[0].author).isInitialized()).toBe(false); | ||
await orm.em.populate(books, ['author']); | ||
|
||
expect(wrap(books[0]).getSchema()).toBe('test'); | ||
expect(wrap(books[0].author).getSchema()).toBe('test'); | ||
expect(wrap(books[0].author).isInitialized()).toBe(true); | ||
expect(books[0].name).toBe('b'); | ||
expect(books[0].author.name).toBe('a'); | ||
}); | ||
|
||
}); |