Skip to content

Commit

Permalink
fix(sql): support uuid like PKs in M:N references (#272)
Browse files Browse the repository at this point in the history
Closes #268
  • Loading branch information
B4nan committed Dec 14, 2019
1 parent a5fc89a commit 2abc19f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/drivers/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
const qb = this.createQueryBuilder(entityName, ctx, true);
const res = await qb.insert(data).execute('run', false);
res.row = res.row || {};
res.insertId = res.insertId || res.row[pk] || data[pk];
res.insertId = data[pk] || res.insertId || res.row[pk];
await this.processManyToMany(entityName, res.insertId, collections, ctx);

return res;
Expand Down
75 changes: 75 additions & 0 deletions tests/issues/GH268.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { unlinkSync } from 'fs';
import { v4 } from 'uuid';

import { Collection, Entity, ManyToMany, MikroORM, PrimaryKey, Property, ReflectMetadataProvider, UuidEntity } from '../../lib';
import { BASE_DIR } from '../bootstrap';
import { SqliteDriver } from '../../lib/drivers/SqliteDriver';

@Entity()
export class A implements UuidEntity<A> {

@PrimaryKey()
uuid: string = v4();

@Property()
name!: string;

@ManyToMany(() => B, b => b.aCollection)
bCollection = new Collection<B>(this);

}

@Entity()
export class B implements UuidEntity<B> {

@PrimaryKey()
uuid: string = v4();

@Property()
name!: string;

@ManyToMany(() => A, undefined, { fixedOrder: true })
aCollection = new Collection<A>(this);

}

describe('GH issue 268', () => {

let orm: MikroORM<SqliteDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [A, B],
dbName: BASE_DIR + '/../temp/mikro_orm_test_gh268.db',
debug: false,
highlight: false,
type: 'sqlite',
metadataProvider: ReflectMetadataProvider,
cache: { enabled: false },
});
await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
});

afterAll(async () => {
await orm.close(true);
unlinkSync(orm.config.get('dbName'));
});

test('m:n with uuid PKs', async () => {
const a1 = new A();
a1.name = 'a1';
const a2 = new A();
a2.name = 'a2';
const a3 = new A();
a3.name = 'a3';
const b = new B();
b.name = 'b';
b.aCollection.add(a1, a2, a3);
await orm.em.persistAndFlush(b);

const res = await orm.em.getConnection().execute('select * from b_to_a');
expect(res[0]).toEqual({ id: 1, a_uuid: a1.uuid, b_uuid: b.uuid });
});

});

0 comments on commit 2abc19f

Please sign in to comment.