You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an primary key column has one or more transformers specified, relations fail to render their sql properly because the do not run the entity value through the transformer.
Expected Behavior
When a relation is loaded, the transformed value present in the entity should be run through the transformer to function(s) before it is passed into the SQL.
As an example, here's a contrived transformer that takes integer database columns and turns them into strings in typescript that have double quotes around them. While obviously not a reasonable use case, there are plenty of reasons why someone may want to transform an Id column in other ways.
Then I expect to be able to call await user.photos and get something like this:
query: SELECT "photos"."id" AS "photos_id", "photos"."url" AS "photos_url", "photos"."userId" AS "photos_userId" FROM "photo" "photos" WHERE "photos"."userId" IN (?) -- PARAMETERS: [1]
Actual Behavior
When a relation is loaded, the transformed value present in the entity is used directly in the SQL without being "untransformed".
In the above example, running await user.photos actually generates this:
query: SELECT "photos"."id" AS "photos_id", "photos"."url" AS "photos_url", "photos"."userId" AS "photos_userId" FROM "photo" "photos" WHERE "photos"."userId" IN (?) -- PARAMETERS: ["\"1\""]
The value for user.id has not been un-transformed before shipping it back to sql.
Notice the final printed query with its incorrect parameters.
My Environment
Dependency
Version
Operating System
debian
Node.js version
12.21.0
Typescript version
4.5.5
TypeORM version
0.2.43
Additional Context
I believe I have identified how to fix the issue. Simply adding a second parameter true to all calls of getEntityValue in RelationLoader.ts. i.e. getEntityValue(entity, true) instead of getEntityValue(entity).
Relevant Database Driver(s)
I have manually confirmed this against postgresql and sqlite, but from my shallow understanding of the codebase, I think this likely affects all databases.
Are you willing to resolve this issue by submitting a Pull Request?
I will be opening a PR shortly. 😄
✅ Yes, I have the time, and I know how to start.
✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
✖️ No, I don’t have the time, but I can support (using donations) development.
✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
The text was updated successfully, but these errors were encountered:
Use column transforms when rendering queries during relation loading. Previously, transforms were ignored by the relation loader, causing unexpected values to be put into sql queries.
Closes: #8690
Issue Description
When an primary key column has one or more transformers specified, relations fail to render their sql properly because the do not run the entity value through the transformer.
Expected Behavior
When a relation is loaded, the transformed value present in the entity should be run through the transformer
to
function(s) before it is passed into the SQL.As an example, here's a contrived transformer that takes integer database columns and turns them into strings in typescript that have double quotes around them. While obviously not a reasonable use case, there are plenty of reasons why someone may want to transform an Id column in other ways.
Then I expect to be able to call
await user.photos
and get something like this:Actual Behavior
When a relation is loaded, the transformed value present in the entity is used directly in the SQL without being "untransformed".
In the above example, running
await user.photos
actually generates this:The value for
user.id
has not been un-transformed before shipping it back to sql.Steps to Reproduce
npm install
npm start
My Environment
Additional Context
I believe I have identified how to fix the issue. Simply adding a second parameter
true
to all calls ofgetEntityValue
inRelationLoader.ts
. i.e.getEntityValue(entity, true)
instead ofgetEntityValue(entity)
.Relevant Database Driver(s)
I have manually confirmed this against postgresql and sqlite, but from my shallow understanding of the codebase, I think this likely affects all databases.
Are you willing to resolve this issue by submitting a Pull Request?
I will be opening a PR shortly. 😄
The text was updated successfully, but these errors were encountered: