0

I have a contact postgres table, with a contacted_date column. I recently created a contact_date table, which is meant to store each date a contact was contacted.

Because the codebase frequently refers to the contact table for the most recent contacted date, I haven't migrated all the data out of that table. Instead, contact_date contains each date a given contact was contacted, and a trigger on that table updates contact with the latest date for each contact.

If I use Sequelize to make a change to contact_date, does the promise await the successful completion of the trigger? Or does it only await the successful completion of the change to contact_date?

In most of my code, this isn't a big concern - if I persist a new contact date, then my code already has access to that date, and I can use that date in other calculations if I need it. But I'm running into difficulties with my tests, and it may be because of a race condition. In my tests, I'm creating new contacts with contact dates, and some of my tests are failing, perhaps because the promise is resolving before the trigger has finished doing its work.

Update: This SO post indicates that the trigger is part of the same transaction, implying that the Sequelize promise wouldn't resolve until the trigger had completed, but if someone knows for sure, I'd still like to hear dis/confirmation.

Community
  • 1
  • 1
Nicholas Barry
  • 558
  • 2
  • 5
  • 15

1 Answers1

1

I hope you're using transactions for your queries, without which I feel this would not be possible.

Also, an alternative way to achieve your objective would be to use hooks (basically a trigger at application level) in sequelize, instead of creating trigger at DB level.
Since you can pass the transaction object to hooks, the operation performed under hooks is guaranteed to be part of the same transaction which caused the hook to run.

Adhyatmik
  • 1,038
  • 11
  • 19
  • I'm using transactions elsewhere, but not yet here. Will that make the difference between the trigger's completion being awaited vs. not being awaited? – Nicholas Barry Apr 19 '17 at 23:01