Skip to content

Commit

Permalink
fix(core): fix hydration of 1:1 FK as PK property via joined strategy
Browse files Browse the repository at this point in the history
Closes #4160
  • Loading branch information
B4nan committed Apr 2, 2023
1 parent 493d653 commit 75653f0
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/entity/EntityHelper.ts
Expand Up @@ -198,7 +198,7 @@ export class EntityHelper {
helper(entity).__pk = helper(entity).getPrimaryKey()!;

// the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
if (value?.[prop2.name as string] != null && value?.[prop2.name as string] !== entity) {
if (!prop.primary && value?.[prop2.name as string] != null && Reference.unwrapReference(value?.[prop2.name as string]) !== entity) {
const other = Reference.unwrapReference(value![prop2.name as string]);
delete helper(other).__data[prop.name];

Expand Down
94 changes: 94 additions & 0 deletions tests/issues/GH4160.test.ts
@@ -0,0 +1,94 @@
import { Collection, Entity, LoadStrategy, ManyToOne, OneToMany, OneToOne, OptionalProps, PrimaryKey, Property, Rel } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/better-sqlite';

@Entity()
class Account {

[OptionalProps]?: 'client';

@PrimaryKey()
id!: string;

@Property()
name!: string;

@OneToOne(() => Client, c => c.account)
client!: Rel<Client>;

}

@Entity()
class Client {

@OneToOne({ primary: true, entity: () => Account })
account!: Account;

@OneToMany(() => Brand, brand => brand.client, { orphanRemoval: true })
brands = new Collection<Brand>(this);

}

@Entity()
class Brand {

@PrimaryKey()
id!: string;

@ManyToOne(() => Client)
client!: Client;

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Client, Account, Brand],
dbName: ':memory:',
});
await orm.schema.refreshDatabase();

const account1 = orm.em.create(Account, { name: 'Account 1', id: '1' });
const client1 = orm.em.create(Client, { account: account1 });
orm.em.create(Brand, { id: '1', client: client1 });
orm.em.create(Brand, { id: '2', client: client1 });
await orm.em.flush();
});

beforeEach(async () => {
orm.em.clear();
});

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

it('populate with select-in strategy', async () => {
const brands1 = await orm.em.find(Brand, {}, {
populate: ['client.account'],
strategy: LoadStrategy.SELECT_IN,
});

expect(brands1[0].client.account).toMatchObject({ id: '1', name: 'Account 1' });
expect(brands1[1].client.account).toMatchObject({ id: '1', name: 'Account 1' });
});

it('populate with joined strategy', async () => {
const brands1 = await orm.em.find(Brand, {}, {
populate: ['client.account'],
strategy: LoadStrategy.JOINED,
});

expect(brands1[0].client.account).toMatchObject({ id: '1', name: 'Account 1' });
expect(brands1[1].client.account).toMatchObject({ id: '1', name: 'Account 1' });
});

it('populate with query builder', async () => {
const brands2 = await orm.em
.createQueryBuilder(Brand, 'brand')
.leftJoinAndSelect('brand.client', 'client')
.leftJoinAndSelect('client.account', 'account');

expect(brands2[0].client.account).toMatchObject({ id: '1', name: 'Account 1' });
expect(brands2[1].client.account).toMatchObject({ id: '1', name: 'Account 1' });
});

0 comments on commit 75653f0

Please sign in to comment.