title: Sequelize: Using truncate and insert within same transaction
body: <p>I need to truncate one of my table and then following it, I need to re populate it using a new dataCollection through bulkCreate. And all this should be within one single transaction I expected this to work simply but I couldn't get this to work, any suggestions how can I use <code>truncate</code> and then <code>bulkCreate</code> on a model in a single transaction?</p>
  
  <p>Here's a snippet of my code</p>
  
  <pre><code>try {
                  promises.push(
                      models.sequelize.transaction(function (t) {
                          return infx.destroy({
                              where: {},
                              truncate: true,
                              transaction: t
                          }).then(function () {
                              return infx.bulkCreate(dataCollection, {
                                  transaction: t
                              });
                          });
                      }).then(function (result) {
                          // Transaction has been committed
                      }).catch(function (err) {
                          sendEmailOnError(err, 0, 0)
                          logger.error(err);
                      })
                  );
              } catch (error) {
                  console.log("Input On End error", error);
                  logger.error(error);
  
              }
              return Promise.all(promises).then(() =&gt; {
                      doMoreThings().then(someData =&gt; {                                                        
                          console.log('Import complete! Shutting down process now.');
                          process.exit(0);
                      })
                  })
                  .catch((e) =&gt; {
                      // console.log("error", e);
                      logger.error(e);
                  });
  </code></pre>
  
  <p>Tried again with async...await, still the transaction doesn't rollback for truncated data.</p>
  
  <blockquote>
    <p>Un-managed transaction</p>
  </blockquote>
  
  <pre><code>        let t;
          try {
              t = await models.sequelize.transaction();
  
              //truncate table
              await infx.destroy({
                  where: {},
                  truncate: true,
                  transaction: t
              });
  
              //insert new bulk collection
              await infx.bulkCreate(dataCollection, {
                  transaction: t
              });
  
              // commit
              await t.commit();
          } catch (error) {
              // console.log("Input On End error", error);
              if (error) await t.rollback();
              sendEmailOnError(error, 0, 0);
              // logger.error(error);
  
          }
  </code></pre>
  
  <blockquote>
    <p>Managed Transaction</p>
  </blockquote>
  
  <pre><code>try {
                  result = await models.sequelize.transaction(
                      async (t) =&gt; {
                          //truncate table
                          await infx.destroy({
                              where: {},
                              truncate: true,
                              transaction: t
                          });
  
                          //insert new bulk collection
                          return await infx.bulkCreate(dataCollection, {
                              transaction: t
                          });
                      });
              } catch (error) {
                  console.log("Input On End error", error);
                  sendEmailOnError(error, 0, 0);
                  // logger.error(error);
              }
  </code></pre>
  
  <blockquote>
    <p>Sequelize log (everything is well executed within transaction)</p>
  </blockquote>
  
  <pre><code>Executing (8f53bd47-e324-48b7-8c7d-ea93dcbdae5c): START TRANSACTION;
  Executing (8f53bd47-e324-48b7-8c7d-ea93dcbdae5c): TRUNCATE `infxes`
  Executing (8f53bd47-e324-48b7-8c7d-ea93dcbdae5c): INSERT INTO `infxes` (...);
  Executing (8f53bd47-e324-48b7-8c7d-ea93dcbdae5c): ROLLBACK;
  </code></pre>