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 removing entity that has an inverse relation with M:1 …
…`owner` property Closes #4578
- Loading branch information
Showing
2 changed files
with
58 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,57 @@ | ||
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, SimpleLogger } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
import { mockLogger } from '../helpers'; | ||
|
||
@Entity() | ||
class User { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@OneToMany(() => Team, team => team.owner) | ||
teams = new Collection<Team>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class Team { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToOne(() => User) | ||
owner?: User; | ||
|
||
} | ||
|
||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Team], | ||
dbName: ':memory:', | ||
loggerFactory: options => new SimpleLogger(options), | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test(`GH issue 4578`, async () => { | ||
orm.em.create(User, {}); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
const [u] = await orm.em.find(User, {}); | ||
|
||
const mock = mockLogger(orm); | ||
await orm.em.removeAndFlush(u); | ||
|
||
expect(mock.mock.calls).toEqual([ | ||
['[query] begin'], | ||
['[query] delete from `user` where `id` in (1)'], | ||
['[query] commit'], | ||
]); | ||
}); |