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

Can't map primary key that is a foreign key to a composite key. #1624

Closed
erik-induro opened this issue Mar 30, 2021 · 7 comments
Closed

Can't map primary key that is a foreign key to a composite key. #1624

erik-induro opened this issue Mar 30, 2021 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@erik-induro
Copy link

erik-induro commented Mar 30, 2021

Describe the bug
I am using postgresql partitioned tables so most of my primary keys are composite keys. When I create a table with a foreign key to a composite primary key it fails to map.

Stack trace

TypeError: Cannot read property 'id' of undefined
    at /Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/utils/Utils.js:369:29
    at Array.map (<anonymous>)
    at Function.getOrderedPrimaryKeys (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/utils/Utils.js:367:33)
    at /Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/utils/Utils.js:371:37
    at Array.map (<anonymous>)
    at Function.getOrderedPrimaryKeys (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/utils/Utils.js:367:33)
    at /Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/utils/Utils.js:371:37
    at Array.map (<anonymous>)
    at Function.getOrderedPrimaryKeys (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/utils/Utils.js:367:33)
    at EntityFactory.findEntity (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/entity/EntityFactory.js:95:35)
    at EntityFactory.create (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/entity/EntityFactory.js:29:29)
    at SqlEntityManager.merge (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/EntityManager.js:364:80)
    at SqlEntityManager.map (/Users/me/projects/sandbox/mikro-orm-fk/node_modules/@mikro-orm/core/EntityManager.js:346:21)
    at test (/Users/me/projects/sandbox/mikro-orm-fk/src/index.ts:39:9)
    at run (/Users/me/projects/sandbox/mikro-orm-fk/src/index.ts:61:2)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

To Reproduce

I created a minimal reproducible example here: https://github.com/erik-induro/mikro-orm-fk

Here are some examples that fail:

// just using the mapper
orm.em.map(UserRole, {
  user_id: 'cc455d1f-f4c7-4b57-b833-e6ca88239b61',
  user_organization_id: 'e3dca7ae-6389-49dc-931d-419716828a79',
  role: Role.admin,
});
// or query and map just the table
await this.em.find(UserRole, { user: { $eq: [userId, orgId] } })
// or try to populate it
await this.em.findOne(
  User,
  { id: { $eq: userId }, organization: { $eq: orgId } },
  { populate: { userRoles: LoadStrategy.JOINED } },
);

Expected behavior

I expect the data to map.

Additional context

If the FK is not the PK then it works fine.

Versions

Dependency Version
node 14.15.3
typescript 4.2.3
mikro-orm 4.5.1
mikro-orm/postgresql 4.5.1
@B4nan B4nan added the bug Something isn't working label Mar 30, 2021
@Langstra
Copy link
Collaborator

From what I see in your code you are missing the following property.

[PrimaryKeyType]: [number, string];

https://mikro-orm.io/docs/composite-keys/#use-case-1-dynamic-attributes

@erik-induro
Copy link
Author

@Langstra I'll add it back in but it doesn't fix the issue.

@B4nan
Copy link
Member

B4nan commented Mar 30, 2021

The code is ok, PrimaryKeyType is just for TS, this is runtime issue.

Already got it reproduced, there are basically two problems, one with the em.map, the other with joined loading strategy (both resulting in same error, but with different code paths and causes).

@B4nan
Copy link
Member

B4nan commented Mar 30, 2021

Lol, ok, just cracked the other issue with joined loading :D

image

@B4nan B4nan closed this as completed in c0c658e Mar 30, 2021
@erik-induro
Copy link
Author

@B4nan thanks for fixing this so quickly!

@B4nan
Copy link
Member

B4nan commented Mar 30, 2021

Things like this have indeed priority! :]

You can try the fix via dev version btw (4.5.2-dev.3)

@erik-induro
Copy link
Author

Thanks I was trying to figure out how to pull it in because I was dead in the water. Looks like it works for me!

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

3 participants