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
It's not persisted to DB, I assign a result of custom .addSelect(...) to it.
But when I generate a migration, typeorm treats it as real column and generates ALTER TABLE ... for it.
So I need a mechanism to ignore a column in migration generation.
Relevant Database Driver(s)
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
spanner
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
The profilePhoto property is not decorated in any way (it's not a column nor a relation), but you can simply specify it and fill the value via the queryBuilder, like in your example.
@alumni This doesn't work if I need to select some primitive data and assign to entity's field. It seems to only work with actual joins. And if I just don't add the column decorator, the field will be stripped out when typeorm deserializes data into entity class instances when I call qb.getMany().
In the last release a bug was introduced, where the virtual column is not automatically selected (workaround: manually add it with addSelect), but hopefully we'll fix this in the next release (there are multiple PRs trying to fix it).
You can add an @AfterLoad()entity listener to your entity that would set the value of your undecorated field every time the entity is loaded. Would that make sense for your use case?
@jovanadjuric No, unfortunately that doesn't work for me, because the data I need to assign to the non-persistent field exists only in the query builder, as a complex select expression.
getMany doesn't work because RawSqlResultsToEntityTransformer only handles properties that is aware of (columns, relations etc.). We use getRawMany instead to get unknown columns.
AfterLoad does not have access to the raw result. You can use it only to calculate something based on the information that is already available in the entity.
The workaround above is a simplified version of what VirtualColumn does (sets mode = 'virtual-property', which BTW, already implies that it's not insertable nor updatable). The special thing about VirtualColumn is that it requires a way to calculate the value (and you can't toggle if it's selectable by default or not).
I'm not sure how selecting is done for the SQLite driver, seems we do a SELECT * which ignores VirtualColumns. Other drivers are explicitly adding the columns to the select clause (including the query for the virtual column).
@alumnigetRawMany implies that I'll have to map raw records into entities myself which is very inconvenient when I have a custom complex query builder, that has multiple nested joins etc.
Thanks for you help anyway.
hi @alexeychikk, had you found the solution?
I'm facing the same issue now. However I don't want to use the @VirtualColumn decorator since it always excecutes when query, which affects the query performance if the virtual column is a complex sql.
Activity
alumni commentedon Feb 7, 2025
Then add the property to the entity class, but don't add the column decorator 😉
A more complex example would be this: https://typeorm.io/select-query-builder#joining-and-mapping-functionality
The
profilePhotoproperty is not decorated in any way (it's not a column nor a relation), but you can simply specify it and fill the value via the queryBuilder, like in your example.alexeychikk commentedon Feb 10, 2025
@alumni This doesn't work if I need to select some primitive data and assign to entity's field. It seems to only work with actual joins. And if I just don't add the column decorator, the field will be stripped out when typeorm deserializes data into entity class instances when I call
qb.getMany().alexeychikk commentedon Feb 10, 2025
I came up with the following workaround:
alumni commentedon Feb 10, 2025
I think the workaround would not be needed. This should work:
Alternatively, you can use a
VirtualColumn(if the formula is always the same):In the last release a bug was introduced, where the virtual column is not automatically selected (workaround: manually add it with
addSelect), but hopefully we'll fix this in the next release (there are multiple PRs trying to fix it).alexeychikk commentedon Feb 10, 2025
@alumni The first option just doesn't work. As I already mentioned above, typeorm strips out all non-column fields. Here is an example sandbox that proves this https://codesandbox.io/p/devbox/heuristic-shirley-85hmd6?file=%2Fsrc%2Fmain.ts%3A60%2C18&workspaceId=ws_C6Tehm4iGBaoLuffpPd45F
Second option is not suitable for me because I need query builder instance.
jovanadjuric commentedon Feb 10, 2025
You can add an
@AfterLoad()entity listener to your entity that would set the value of your undecorated field every time the entity is loaded. Would that make sense for your use case?alexeychikk commentedon Feb 10, 2025
@jovanadjuric No, unfortunately that doesn't work for me, because the data I need to assign to the non-persistent field exists only in the query builder, as a complex select expression.
alumni commentedon Feb 10, 2025
getManydoesn't work becauseRawSqlResultsToEntityTransformeronly handles properties that is aware of (columns, relations etc.). We usegetRawManyinstead to get unknown columns.AfterLoaddoes not have access to the raw result. You can use it only to calculate something based on the information that is already available in the entity.The workaround above is a simplified version of what
VirtualColumndoes (setsmode = 'virtual-property', which BTW, already implies that it's not insertable nor updatable). The special thing aboutVirtualColumnis that it requires a way to calculate the value (and you can't toggle if it's selectable by default or not).I'm not sure how selecting is done for the SQLite driver, seems we do a
SELECT *which ignoresVirtualColumns. Other drivers are explicitly adding the columns to the select clause (including the query for the virtual column).alexeychikk commentedon Feb 10, 2025
@alumni
getRawManyimplies that I'll have to map raw records into entities myself which is very inconvenient when I have a custom complex query builder, that has multiple nested joins etc.Thanks for you help anyway.
TLCRATEAM commentedon Aug 1, 2025
hi @alexeychikk, had you found the solution?
I'm facing the same issue now. However I don't want to use the
@VirtualColumndecorator since it always excecutes when query, which affects the query performance if the virtual column is a complex sql.alexeychikk commentedon Aug 1, 2025
@TLCRATEAM I use this:
TLCRATEAM commentedon Aug 1, 2025
@alexeychikk ah thank you
I choose this approach (custom decorator) as well :D
alumni commentedon Aug 2, 2025
@VirtualColumnis the correct answer. Will allow the users to make it initially non-selectable in #11586.TLCRATEAM commentedon Aug 4, 2025
Thank you @alumni .
Good to hear that.