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

ChangeSetPersister#mapReturnedValues does not handle OneToOne navigation properties #501

Closed
Haringat opened this issue Apr 21, 2020 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@Haringat
Copy link

Describe the bug
After persisting an entity, mapReturnedValues assigned the primaryKey value (in my case a string) to the OneToOne (primary: true) property (the type is of course the other Entity class) of the Entity object, thus triggering the set() which mikro-orm assigned and EntityHelper.propagate threw the error below.

Stack trace

TypeError: wrap(...).isInitialized is not a function
    at Function.propagate (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\entity\EntityHelper.js:171:91)
    at Gateway.set [as device] (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\entity\EntityHelper.js:159:30)
    at C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\unit-of-work\ChangeSetPersister.js:87:39
    at Array.forEach (<anonymous>)
    at ChangeSetPersister.mapReturnedValues (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\unit-of-work\ChangeSetPersister.js:85:44)
    at ChangeSetPersister.persistEntity (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\unit-of-work\ChangeSetPersister.js:33:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ChangeSetPersister.persistToDatabase (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\unit-of-work\ChangeSetPersister.js:20:9)
    at async UnitOfWork.commitChangeSet (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\unit-of-work\UnitOfWork.js:208:9)
    at async UnitOfWork.persistToDatabase (C:\Git\etaplus-connect-brick4u\node_modules\.pnpm\localhost+4873\mikro-orm\3.6.9_pg@7.18.2\node_modules\mikro-orm\dist\unit-of-work\UnitOfWork.js:342:13)

To Reproduce
Steps to reproduce the behavior:

  1. Create two entities which share a OneToOne relation to each other
  2. Make the OneToOne-Decorated property the primary key for one of them
  3. Instantiate both classes and try to persist them

Expected behavior
The entities are saved to the database

Additional context
I used PostgreSQL driver and ReflectMetadataProvider.

Maybe a duplicate of #465

Versions

Dependency Version
node 13.9.0
typescript 3.8.3
mikro-orm 3.6.9
your-driver pg (7.18.2)
@Haringat Haringat added the bug Something isn't working label Apr 21, 2020
@B4nan
Copy link
Member

B4nan commented Apr 21, 2020

Is that entity discovered?

Also this is not enough as a reproduction, what you described is definitely working, there are tests for this use case.

@Haringat
Copy link
Author

Haringat commented Apr 21, 2020

@B4nan It might be connected to the PostgreSQL driver (the test is for MySQL). I will see if I can create a minimal reproduction.

@B4nan
Copy link
Member

B4nan commented Apr 21, 2020

I doubt it has anything to do with drivers, the error is caused by missing method that is added to entity prototype during discovery. So either the variable is holding an entity that was not discovered, or it does not hold entity instance at all. Try putting there console.log() to see what the parameter of wrap() is.

Maybe you have this problem? #431

@Haringat
Copy link
Author

Maybe you have this problem? #431

No, I used the primary: true attribute rather than the @PrimaryKey() decorator on the owner.

@Haringat
Copy link
Author

Haringat commented Apr 22, 2020

@B4nan
It seems my intuition was correct. I copied the test suite you referenced and just changed

  let orm: MikroORM<MySqlDriver>;

  beforeAll(async () => orm = await initORMMySql());
  beforeEach(async () => wipeDatabaseMySql(orm.em));

into

  let orm: MikroORM<PostgreSqlDriver>;

  beforeAll(async () => orm = await initORMPostgreSql());
  beforeEach(async () => wipeDatabasePostgreSql(orm.em));

at the top of the file. And it threw the exact same error

TypeError: wrap(...).isInitialized is not a function

    at Function.propagate (/home/marcel/projects/mikro-orm/lib/entity/EntityHelper.ts:206:79)
    at Address2.set [as author] (/home/marcel/projects/mikro-orm/lib/entity/EntityHelper.ts:191:22)
    at /home/marcel/projects/mikro-orm/lib/unit-of-work/ChangeSetPersister.ts:105:28
    at Array.forEach (<anonymous>)
    at ChangeSetPersister.mapReturnedValues (/home/marcel/projects/mikro-orm/lib/unit-of-work/ChangeSetPersister.ts:103:54)
    at ChangeSetPersister.persistEntity (/home/marcel/projects/mikro-orm/lib/unit-of-work/ChangeSetPersister.ts:37:12)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at ChangeSetPersister.persistToDatabase (/home/marcel/projects/mikro-orm/lib/unit-of-work/ChangeSetPersister.ts:24:5)
    at UnitOfWork.commitChangeSet (/home/marcel/projects/mikro-orm/lib/unit-of-work/UnitOfWork.ts:262:5)
    at UnitOfWork.persistToDatabase (/home/marcel/projects/mikro-orm/lib/unit-of-work/UnitOfWork.ts:420:7)

@B4nan B4nan closed this as completed in 99a4f01 Apr 22, 2020
@Haringat
Copy link
Author

@B4nan Thank you for fixing this.

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