Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read properties of undefined (reading '__data') on entity creation with one-to-one relation #4254

Closed
denis-maiorov-brightsec opened this issue Apr 21, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@denis-maiorov-brightsec
Copy link

denis-maiorov-brightsec commented Apr 21, 2023

Describe the bug
Creating and persisting entities with OneToOne relation on Entity that uses owning Entity primary key as their primary.
Worked with 5.5.0 version, fails with 5.6.16

Stack trace

/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityHelper.js:174
            delete (0, wrap_1.helper)(other).__data[prop.name];
                                            ^

TypeError: Cannot read properties of undefined (reading '__data')
    at Function.propagateOneToOne (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityHelper.js:174:45)
    at Function.propagate (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityHelper.js:162:30)
    at OrderEntity.set [as summary] (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityHelper.js:145:30)
    at OrderEntity.set (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityHelper.js:81:41)
    at eval (eval at createFunction (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/utils/Utils.js:780:20), <anonymous>:13:22)
    at Function.callCompiledFunction (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/utils/Utils.js:791:20)
    at ObjectHydrator.hydrate (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/hydration/ObjectHydrator.js:26:23)
    at EntityFactory.hydrate (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityFactory.js:187:27)
    at EntityFactory.create (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/entity/EntityFactory.js:53:14)
    at SqlEntityManager.create (/Users/domayorov/brightsec/mikro-orm-one-to-one-persist-bug/node_modules/@mikro-orm/core/EntityManager.js:826:41)

To Reproduce
Steps to reproduce the behavior:

  1. Create owning Entity and inverse Entity
  2. Associate entities using OneToOne with inversed Entity using owning Entity id as primary key
  3. Try to create new entity with prefilled primary keys and persist

Reproduction example: https://github.com/denis-maiorov-brightsec/mikro-orm-4254

Expected behavior
Both entities are persisted

Additional context
Runtime error shown in stack trace section

Versions

Dependency Version
node 16
typescript 4.8.3
mikro-orm 5.6.16
your-driver postgresql
@B4nan
Copy link
Member

B4nan commented Apr 21, 2023

Please provide complete reproduction as code, I need to see what exactly are you doing, how your entities are defined as well as the ORM configuration.

@denis-maiorov-brightsec
Copy link
Author

@B4nan updated original comment, here's repo with reproduction https://github.com/denis-maiorov-brightsec/mikro-orm-4254

@B4nan
Copy link
Member

B4nan commented Apr 21, 2023

One quick note - you dont need to call em.persist, that's handled by em.create automatically, and you can use it to create the full entity graph in one step:

orm.em.create(OrderEntity, {
  id: 4,
  summary: {
    orderId: 4,
    prop: '123',
  },
});
await orm.em.flush();

I guess it will be caused by the mapToPk option.

@B4nan B4nan added the bug Something isn't working label Apr 21, 2023
@B4nan B4nan closed this as completed in 7028890 Apr 21, 2023
jsprw pushed a commit to jsprw/mikro-orm-full-text-operators that referenced this issue May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants