Skip to content

MapToPk 1:1 #1124

@reramjiawan

Description

@reramjiawan

Describe the bug
According to docs we can use mapToPk option on M:1 and 1:1 relations and it does not work for 1:1

Stack trace

  console.error
    JIT runtime error: Cannot read property '__initialized' of undefined
    
    function(entity, data, factory, newEntity, convertCustomTypes) {
      if (data.id === null) {
        entity.id = null;
      } else if (typeof data.id !== 'undefined') {
        if (isPrimaryKey(data.id, true)) {
          entity.id = data.id;
        } else if (data.id && typeof data.id === 'object') {
          entity.id = data.id;
        }
      }
      if (entity.id && !entity.id.entity) {
        entity.id.entity = entity;
      }
    }

      724 |       if ([SyntaxError, TypeError, EvalError, ReferenceError].some(t => e instanceof t)) {
      725 |         // eslint-disable-next-line no-console
    > 726 |         console.error(`JIT runtime error: ${e.message}\n\n${fn.toString()}`);
          |                 ^
      727 |       }
      728 | 
      729 |       throw e;

      at Function.callCompiledFunction (packages/core/src/utils/Utils.ts:726:17)
      at ObjectHydrator.hydrateReference (packages/core/src/hydration/ObjectHydrator.ts:32:11)
      at EntityFactory.hydrate (packages/core/src/entity/EntityFactory.ts:113:21)
      at EntityFactory.create (packages/core/src/entity/EntityFactory.ts:50:10)
      at EntityFactory.createReference (packages/core/src/entity/EntityFactory.ts:84:17)
      at eval (eval at createFunction (packages/core/src/utils/Utils.ts:711:46), <anonymous>:7:31)
      at Function.callCompiledFunction (packages/core/src/utils/Utils.ts:722:14)

To Reproduce

import { Entity, Logger, MikroORM, OneToOne, PrimaryKey } from '@mikro-orm/core';
import { AbstractSqlDriver } from '@mikro-orm/knex';

@Entity()
export class B {

  @OneToOne({ entity: 'A', joinColumn: 'id', primary: true, mapToPk: true  })
  id!: number;

}

@Entity()
export class A {

  @PrimaryKey()
  id!: number;

  @OneToOne({ entity: 'B', mappedBy: 'id' })
  entity!: B;

}

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

  let orm: MikroORM<AbstractSqlDriver>;
  const log = jest.fn();

  beforeAll(async () => {
    orm = await MikroORM.init({
      entities: [A, B],
      dbName: ':memory:',
      type: 'sqlite',
    });
    const logger = new Logger(log, ['query', 'query-params']);
    Object.assign(orm.config, { logger });

    await orm.getSchemaGenerator().createSchema();

    const a = orm.em.create(A, {
      entity: new B(),
    });

    await orm.em.persistAndFlush(a);
    orm.em.clear();
  });

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

  afterAll(() => orm.close(true));

  test('According to docs we can use mapToPk option on M:1 and 1:1 relations and it does not work for 1:1', async () => {
    const entity = await orm.em.findOne(A, { id: 1 }) as A;

    expect(entity.entity).toContain({ id: 1 });
  });
});

Expected behavior
Be able to use mapToPk option on 1:1 relations

Versions

Dependency Version
node 12.18.4
typescript 4.0.3
mikro-orm 4.3.1
mikro-orm/mysql 4.3.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions