-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
JSON Keys break RawResult to EntityTransformer #6833
Comments
I think we can improve this solution... Somehow. Not sure how exactly... JSONB and JSON aren't the same - least of which is the on-disk serialization format. Duplicate keys, unsorted keys, etc, will cause differences between the two as well, which this will not handle. |
Could you elaborate what you mean by Duplicate / unsorted keys and how this would not be handled? I'm totally open to improve the solution. |
I've added tests for this issue in #6833 as well |
Sure - if you flip the keys around it should fail to find them but technically it's the same thing. Both to JSONB and as parsed JSON.
Because JSONB won't store it as you wrote it - it should sort the keys - this should fail if it were a test. |
Understood. I've added a test for this and it seems as if the order does not matter: https://github.com/typeorm/typeorm/pull/6834/files#diff-09760a400d18a7e291718cb6c8f624b9R69 |
When using PrimaryColumn with a 'jsonb' type column ( on a postgres database ) the RawSqlResultsToEntityTransformer would simply convert the key to '[object Object]' instead of a unique value based on the key value. It would then wrongly consider each entity as having the same key. To allow for complex keys the transformer now uses JSON.stringify() to convert the keys value if they are of type obejct to a unique string that can be compared to other entities. fixes typeorm#6833
* fix: allow for complex jsonb primary key columns When using PrimaryColumn with a 'jsonb' type column ( on a postgres database ) the RawSqlResultsToEntityTransformer would simply convert the key to '[object Object]' instead of a unique value based on the key value. It would then wrongly consider each entity as having the same key. To allow for complex keys the transformer now uses JSON.stringify() to convert the keys value if they are of type obejct to a unique string that can be compared to other entities. fixes typeorm#6833 * test: add tests for jsonb primarykey columns These tests verify that Entities with jsonb primary columns are correctly converted to Entites
( I've provided a PR for this issue here: #6834 )
For the following entity:
With the following data:
The following code will only return one entity:
This is due to:
typeorm/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts
Line 66 in 5ef9450
Adding
solves the issue
The text was updated successfully, but these errors were encountered: