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
{{ message }}
Repository navigation
EventSubscriber not getting updated values for update using queryBuilder and manager.update methods. #2246
[ ] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
Whenever entity is update using queryBuilder update method or entity update method, the EventSubscriber's afterUpdate method gets triggered, but the UpdateEvents entity, databaseEntity, updatedColumns, updateRelations are always empty. These fields are populated only when the updates are done using save methods. Is this intended behaviour.
Reacted by Konstantin Bezgodov, Ihor Sakailiuk, Jerome S., Piotr, Artem Sydorenko, Chris Oliver, Kostayne, Santiago U and Felipe Lima
@pleerock i think I runned into the same issue. I have a beforeUpdate method in an EntitySubscriber. The method is called correctly but in side the event the .entity property is undefined.
I think this applies for beforeInsert, afterInsert, beforeUpdate, afterUpdate, beforeRemove, afterRemove too. Only afterLoad has the entity (maybe because the signature is different).
I'm not too familiar with chai, so I didn't know how to provide a spy for the ParkingSpotEntitySubscriber. And having expect statements in the actual implementation (ParkingSpotEntitySubscriber.ts) felt too fragile too. So i created global vars for testing.. Anyway, it proves the case: event.entity is undefined in ParkingSpotEntitySubscriber#beforeUpdate
Listeners are confusing but because of flexibility and functionality orm provides. With save you can always have an entity or partial entity which you insert/update. With update and update query builder you have more like a set of values to be used in UPDATE query. For example:
update({id: 1,counter: ()=>"counter + 1"});
counter in entity is actually a number, but this is a valid syntax for update, and if we pass this update set to listener we can't call it entity anymore.
TypeORM is huge and has lot of small functionality and lot of quirks. There are hundred such small details all of them simply can't be documented not only because its impossible to keep in mind all small facts like this, but also because it will complicate documentation. Maybe I'll add this to documentation in the future.
Reacted by Pavan Rajendra Bahuguni
Reacted by Anton Bessonov, Michael Donahue, Ihor Sakailiuk, Jake Malone, Karol Walasek, Pablo Berganza, Michael, Pierre Marchal, Dmitry, Jerome S. and 148 more
wow, wow so many dislikes on my above post ^ Okay, okay, now I definitely know we need to add this to the docs.
Reacted by mnariDatafas and Mateusz Kacała
Reacted by Giorgi Tvalmikhelashvili, Hendrix Roa, Akinwunmi Oluwaseun, Lucas Fontinele, Karl Adler, Jimmy, Dakota Cookenmaster, Luis dos Reis, Matthias Chometon, Himel Nag Rana and 5 more
Reacted by Can Kattwinkel, Pavan Rajendra Bahuguni, Angelos Pikoulas, Egor Makarenko, Darren Hickling, Burak Kavak, Pavan Agrawal, Dave, daweed, Allan Pinheiro de Lima and 68 more
But then, what's the preferred approach to reliably act upon update of an entity, with data about the entity itself available? By reliably, I mean no matter of the way the data is updated, whether is .save() or .update(), or query builder or whatever.
I don't want to be forced to remember to use certain way of updating just because somewhere on the other end of the repository there's a subscriber that reacts differently to different methods.
Reacted by Lucianna Almeida, John Musa, Nenad Knezev, Danny Matkovsky, Clinton Blackburn, José Chávez, ordalbang, Masoud Salehi, Rasmus Løvstad, Enger Colman Terseer and 5 more
I don't want to be forced to remember to use certain way of updating just because somewhere on the other end of the repository there's a subscriber that reacts differently to different methods.
I second this!
Is there any other safer method to get the old values of my entity in my subscriber? I tried to use event.manager to fetch the old values from database, but it seems to be using the same transaction the update is using.
I don't want to be forced to remember to use certain way of updating just because somewhere on the other end of the repository there's a subscriber that reacts differently to different methods.
I second this!
Is there any other safer method to get the old values of my entity in my subscriber? I tried to use event.manager to fetch the old values from database, but it seems to be using the same transaction the update is using.
Listeners are confusing but because of flexibility and functionality orm provides. With save you can always have an entity or partial entity which you insert/update. With update and update query builder you have more like a set of values to be used in UPDATE query. For example:
update({id: 1,counter: ()=>"counter + 1"});
counter in entity is actually a number, but this is a valid syntax for update, and if we pass this update set to listener we can't call it entity anymore.
if I set valid value of column, when call update , subscriber get entity is undefined, this is unfriendly.
I don't want to be forced to remember to use certain way of updating just because somewhere on the other end of the repository there's a subscriber that reacts differently to different methods.
I second this!
Is there any other safer method to get the old values of my entity in my subscriber? I tried to use event.manager to fetch the old values from database, but it seems to be using the same transaction the update is using.
@pleerock have you had the time to documented this? I didn't find anything on the documentation and I still struggle to understand when the subscriber is actually called. If I use a QueryBuilder, then the afterUpdate is called without data. Same as using Entity.update. Then If I use Entity.save it does get called, but only with the values I set in the entity instance
In my case, I need to use the QueryBuilder or the update since I need to return the completed entity values after the update. What would be the best to do this today?
@tafelito@pleerock as mentioned above I'm struggling on same thing. We can't use save() all the time, at times based on certain scenarios we have to use update().
How to tackle this?
I am getting empty arrays for the following event.updatedColumns: [] , event.updatedRelations: [] . I have tried using both.update(), connection.query() with manual SQL and repositoryManager to update. All resulted in no metadata to use.
What worked for me is using .save() to trigger afterUpdate subscription. From the docs: "If you run save without modifying anything from the model, @BeforeUpdate and @AfterUpdate will not run."
const order = await Order.findOne(id);
if (!order) throw new ApolloError("No order found", "NOT_FOUND");
order.baseCurrency = "SEK";
order?.save();
TypeORM gives you lots of control over your database interactions. You can use high-level, fully-featured but less efficient operations, or low-level and fast operations.
When executing high-level operations (EntityManager.save), TypeORM has full access to the entity and can do additional queries to determine whether inserting or updating. Therefore TypeORM can call subscribers with entities.
When executing primitive operations ( EntityManager.insert, EntityManager.update, EntityManager.delete), TypeORM does not always have access to entities, but does know the operation. Therefore TypeORM can call subscribers, but cannot always provide the entities.
When executing raw SQL queries (QueryRunner.query), TypeORM does not know what you're doing and doesn't have access to entities or subscribers.
Each method has tradeoffs. You have to decide the best fit for your use case.
If you want to see exactly what the orm is doing, turn on logging and check out the SQL being executed for your operations
Reacted by Dries Croons, Preet Parekh, Tal, Joe Wong, shibata, hungvu-xenera, ordalbang, Florian CHAUX, Jonghun Yu, 서보민 and 2 more
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.
Activity
pleerock commentedon Jun 1, 2018
any code example?
CanKattwinkel commentedon Jun 3, 2018
@pleerock i think I runned into the same issue. I have a beforeUpdate method in an EntitySubscriber. The method is called correctly but in side the event the .entity property is undefined.
I think this applies for
beforeInsert, afterInsert, beforeUpdate, afterUpdate, beforeRemove, afterRemovetoo. Only afterLoad has the entity (maybe because the signature is different).console.logs after update, then load the entity:
E: "version": "0.2.6",
adds test for typeorm#2246
CanKattwinkel commentedon Jun 3, 2018
I tried to provide a test case covering the issue in a fork:
https://github.com/CanKattwinkel/typeorm/blob/master/test/github-issues/2246/issue-2246.ts
I'm not too familiar with chai, so I didn't know how to provide a spy for the ParkingSpotEntitySubscriber. And having expect statements in the actual implementation (ParkingSpotEntitySubscriber.ts) felt too fragile too. So i created global vars for testing.. Anyway, it proves the case:
event.entityis undefined inParkingSpotEntitySubscriber#beforeUpdatepleerock commentedon Jun 4, 2018
what if you use
saveinstead ofupdate?CanKattwinkel commentedon Jun 4, 2018
@pleerock then it works - is this the desired behavior? Because this feels confusing.
pleerock commentedon Jun 4, 2018
Listeners are confusing but because of flexibility and functionality orm provides. With
saveyou can always have an entity or partial entity which you insert/update. Withupdateandupdate query builderyou have more like a set of values to be used in UPDATE query. For example:counterin entity is actually a number, but this is a valid syntax forupdate, and if we pass this update set to listener we can't call itentityanymore.CanKattwinkel commentedon Jun 4, 2018
Thanks for your patience, I get it now.
Since this is not quite obvious, should it be mentioned in the docs?
https://github.com/typeorm/typeorm/blob/master/docs/listeners-and-subscribers.md#beforeupdate
pleerock commentedon Jun 4, 2018
TypeORM is huge and has lot of small functionality and lot of quirks. There are hundred such small details all of them simply can't be documented not only because its impossible to keep in mind all small facts like this, but also because it will complicate documentation. Maybe I'll add this to documentation in the future.
pleerock commentedon Feb 25, 2019
wow, wow so many dislikes on my above post ^ Okay, okay, now I definitely know we need to add this to the docs.
jalooc commentedon Nov 15, 2019
But then, what's the preferred approach to reliably act upon update of an entity, with data about the entity itself available? By reliably, I mean no matter of the way the data is updated, whether is
.save()or.update(), or query builder or whatever.I don't want to be forced to remember to use certain way of updating just because somewhere on the other end of the repository there's a subscriber that reacts differently to different methods.
vinimdocarmo commentedon Dec 10, 2019
I second this!
Is there any other safer method to get the old values of my entity in my subscriber? I tried to use
event.managerto fetch the old values from database, but it seems to be using the same transaction the update is using.or2008 commentedon Dec 20, 2019
+1
HunterJS-bit commentedon Dec 26, 2019
@pleerock
But how can I then get updated value, or proprties that are updated ?
muyoucun557 commentedon Mar 20, 2020
if I set valid value of column, when call
update, subscriber get entity is undefined, this is unfriendly.muyoucun557 commentedon Mar 20, 2020
call
savezeele commentedon Jul 26, 2020
+1
tafelito commentedon Aug 2, 2020
@pleerock have you had the time to documented this? I didn't find anything on the documentation and I still struggle to understand when the subscriber is actually called. If I use a QueryBuilder, then the afterUpdate is called without data. Same as using Entity.update. Then If I use Entity.save it does get called, but only with the values I set in the entity instance
In my case, I need to use the QueryBuilder or the update since I need to return the completed entity values after the update. What would be the best to do this today?
sandeshgb commentedon Aug 12, 2020
@tafelito @pleerock as mentioned above I'm struggling on same thing. We can't use save() all the time, at times based on certain scenarios we have to use update().
How to tackle this?
vemundeldegard commentedon Aug 25, 2020
I am getting empty arrays for the following
event.updatedColumns: [] , event.updatedRelations: []. I have tried using both.update(),connection.query()with manual SQL and repositoryManager to update. All resulted in no metadata to use.What worked for me is using .save() to trigger afterUpdate subscription. From the docs: "If you run save without modifying anything from the model, @BeforeUpdate and @AfterUpdate will not run."
NickKelly1 commentedon Aug 28, 2020
TypeORM gives you lots of control over your database interactions. You can use high-level, fully-featured but less efficient operations, or low-level and fast operations.
EntityManager.save), TypeORM has full access to the entity and can do additional queries to determine whether inserting or updating. Therefore TypeORM can call subscribers with entities.EntityManager.insert,EntityManager.update,EntityManager.delete), TypeORM does not always have access to entities, but does know the operation. Therefore TypeORM can call subscribers, but cannot always provide the entities.QueryRunner.query), TypeORM does not know what you're doing and doesn't have access to entities or subscribers.Each method has tradeoffs. You have to decide the best fit for your use case.
If you want to see exactly what the orm is doing, turn on logging and check out the SQL being executed for your operations
driescroons commentedon Sep 10, 2020
@NickKelly1 you can call the save method instead of the update method

karladler commentedon Dec 10, 2020
maybe the listenerMethod should just be renamed (+ aliased for backwards compatibility) to
afterSaveto avoid this confusion.shaqqan commentedon Jul 23, 2024
github-actions commentedon Sep 18, 2025
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.