Skip to content

Commit

Permalink
fix(core): fix removing entity that has an inverse relation with M:1 …
Browse files Browse the repository at this point in the history
…`owner` property

Closes #4578
  • Loading branch information
B4nan committed Aug 9, 2023
1 parent 9f0db10 commit fbed4a6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/unit-of-work/UnitOfWork.ts
Expand Up @@ -438,7 +438,7 @@ export class UnitOfWork {
}

// there is a value, and it is still a self-reference (e.g. not replaced by user manually)
if (rel?.[inverse] && entity === Reference.unwrapReference(rel[inverse])) {
if (!Utils.isCollection(rel) && rel?.[inverse] && entity === Reference.unwrapReference(rel[inverse])) {
delete helper(rel).__data[inverse];
}
}
Expand Down
57 changes: 57 additions & 0 deletions tests/issues/GH4578.test.ts
@@ -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'],
]);
});

0 comments on commit fbed4a6

Please sign in to comment.