Skip to content

Commit

Permalink
fix(core): fix removing of m:n items when one is composite
Browse files Browse the repository at this point in the history
Closes #1961
  • Loading branch information
B4nan committed Jun 29, 2021
1 parent 044fd68 commit 81c0b30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 1 addition & 3 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,6 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
}

protected async updateCollectionDiff<T extends AnyEntity<T>, O extends AnyEntity<O>>(meta: EntityMetadata<O>, prop: EntityProperty<T>, pks: Primary<O>[], deleteDiff: Primary<T>[][] | boolean, insertDiff: Primary<T>[][], ctx?: Transaction): Promise<void> {
const meta2 = this.metadata.find<T>(prop.type)!;

if (!deleteDiff) {
deleteDiff = [];
}
Expand All @@ -625,7 +623,7 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
knex.whereIn(prop.inverseJoinColumns, deleteDiff as Knex.Value[][]);
}

meta2.primaryKeys.forEach((pk, idx) => knex.andWhere(prop.joinColumns[idx], pks[idx] as Knex.Value[][]));
prop.joinColumns.forEach((joinColumn, idx) => knex.andWhere(joinColumn, pks[idx] as Knex.Value[][]));
await this.execute(knex.delete());
}

Expand Down
22 changes: 22 additions & 0 deletions tests/features/composite-keys/composite-keys.sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PrimaryKeyType, Property, ValidationError, wrap, LoadStrategy, Logger,
} from '@mikro-orm/core';
import { AbstractSqlConnection, SqliteDriver } from '@mikro-orm/sqlite';
import { mockLogger } from '../../bootstrap';

@Entity()
export class FooBar2 {
Expand Down Expand Up @@ -476,6 +477,27 @@ describe('composite keys in sqlite', () => {
expect(c3).toBeNull();
});

test('removing composite entity in m:n relationship, one of entity is composite (GH #1961)', async () => {
const sandwich1 = new Sandwich('Fish Sandwich', 100);
const sandwich2 = new Sandwich('Fried Egg Sandwich', 200);
const sandwich3 = new Sandwich('Grilled Cheese Sandwich', 300);
const user1 = new User2('Henry', 'Doe 1');
const user2 = new User2('Henry', 'Doe 2');
const user3 = new User2('Henry', 'Doe 3');
user1.sandwiches.add(sandwich1, sandwich3);
user2.sandwiches.add(sandwich3);
user2.sandwiches.add(sandwich2, sandwich3);
await orm.em.persistAndFlush([user1, user2, user3]);

const mock = mockLogger(orm);
user2.sandwiches.removeAll();
await orm.em.flush();

expect(mock.mock.calls[0][0]).toMatch('begin');
expect(mock.mock.calls[1][0]).toMatch('delete from `user2_sandwiches` where (`sandwich_id`) in ( values (2), (3)) and `user2_first_name` = \'Henry\' and `user2_last_name` = \'Doe 2\'');
expect(mock.mock.calls[2][0]).toMatch('commit');
});

test('composite entity in m:n relationship, one of entity is composite', async () => {
const sandwich1 = new Sandwich('Fish Sandwich', 100);
const sandwich2 = new Sandwich('Fried Egg Sandwich', 200);
Expand Down

0 comments on commit 81c0b30

Please sign in to comment.