Skip to content

Commit

Permalink
fix(core): fix extracting PK out of reference wrapper
Browse files Browse the repository at this point in the history
Closes #589
  • Loading branch information
B4nan committed Jun 1, 2020
1 parent 9d9c3a2 commit 1fd07b3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/utils/Utils.ts
Expand Up @@ -277,7 +277,7 @@ export class Utils {
return data as Primary<T>;
}

if (Utils.isEntity(data) && !meta) {
if (Utils.isEntity(data, true) && !meta) {
meta = wrap(data, true).__meta;
}

Expand Down
79 changes: 79 additions & 0 deletions tests/issues/GH589.test.ts
@@ -0,0 +1,79 @@
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, PrimaryKeyType, Reference, IdentifiedReference } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';

@Entity()
export class User {

@PrimaryKey()
id!: number;

@OneToMany('Chat', 'owner')
ownedChats = new Collection<Chat>(this);

}

@Entity()
export class Chat {

@ManyToOne(() => User, { primary: true, wrappedReference: true })
owner: IdentifiedReference<User>;

@ManyToOne(() => User, { primary: true, wrappedReference: true })
recipient: IdentifiedReference<User>;

[PrimaryKeyType]: [number, number];

constructor(owner: User, recipient: User) {
this.owner = Reference.create(owner);
this.recipient = Reference.create(recipient);
}

}

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

let orm: MikroORM<PostgreSqlDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User, Chat],
dbName: `mikro_orm_test_gh_589`,
type: 'postgresql',
});
await orm.getSchemaGenerator().ensureDatabase();
await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
});

afterAll(async () => {
await orm.close(true);
});

beforeEach(() => orm.em.clear());

test(`GH issue 589 (originally working)`, async () => {
const user1 = new User();
await orm.em.persistAndFlush(user1);
const user2 = new User();
await orm.em.persistAndFlush(user2);
const chat1 = new Chat(user1, user2);
orm.em.persist(chat1);
const chat2 = new Chat(user2, user1);
orm.em.persist(chat2);

await expect(orm.em.flush()).resolves.toBeUndefined();
});

test(`GH issue 589 (originally failing)`, async () => {
const user3 = new User();
await orm.em.persistAndFlush(user3);
const user4 = new User();
await orm.em.persistAndFlush(user4);
const chat3 = new Chat(user3, user4);
await orm.em.persistAndFlush(chat3);
const chat4 = new Chat(user4, user3);
orm.em.persist(chat4);

await expect(orm.em.flush()).resolves.toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1fd07b3

Please sign in to comment.