MySQL ROLLBACK 不删除新记录-sequelize

标签 mysql node.js sequelize.js mysql-5.7

我正在使用sequelize v5.1.0 在 MySQL 5.7.25-0ubuntu0.18.04.2 中创建事务。它似乎正在根据MySQL 5.7 Documentation执行正确的命令。但之后插入并回滚的记录仍然存在于数据库中。

javascript

let promises = []
models.sequelize.transaction(function (t) {
  promises.push(models.alert.create(setter, { transaction: t }))
  promises.push(new Promise((resolve, reject) => {
    reject(new Error('roll it back yall'))
  }))
  return Promise.all(promises)
}).then(function () {
  console.log('SUCCESS!!! (will commit)')
}).catch(function (err) {
  console.log('FAILURE !!! (will rollback)')
  next(err)
})

SQL查询日志

| 2019-03-21 12:55:17.798200 | root[root] @  [10.211.55.2] |      2151 |         0 | Query        | START TRANSACTION                                                                                                                                                                                                                                                                         |
| 2019-03-21 12:55:19.597304 | root[root] @  [10.211.55.2] |      2151 |         0 | Prepare      | INSERT INTO `alerts` (`id`,`user_id`,`alert_name`,`reading_type`,`reading_condition`,`reading_value`,`always_active`,`sensors_global`,`enabled`,`last_updated`,`updated`) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?)                                                                            |
| 2019-03-21 12:55:19.616278 | root[root] @  [10.211.55.2] |      2151 |         0 | Execute      | INSERT INTO `alerts` (`id`,`user_id`,`alert_name`,`reading_type`,`reading_condition`,`reading_value`,`always_active`,`sensors_global`,`enabled`,`last_updated`,`updated`) VALUES (DEFAULT,21,'Test Alert','temperature','below',60,1,0,1,'2019-03-21 12:55:17.781','2019-03-21 12:55:17') |
| 2019-03-21 12:55:19.619249 | root[root] @  [10.211.55.2] |      2151 |         0 | Query        | ROLLBACK

在数据库中记录

mysql> select * from alerts where alert_name='Test Alert';
+-------+---------+------------+--------------+-------------------+---------------+---------------+---------------+----------------+---------+---------------------+---------------------+
| id    | user_id | alert_name | reading_type | reading_condition | reading_value | alert_message | always_active | sensors_global | enabled | updated             | last_updated        |
+-------+---------+------------+--------------+-------------------+---------------+---------------+---------------+----------------+---------+---------------------+---------------------+
| 48689 |      21 | Test Alert | temperature  | below             |         60.00 | NULL          |             1 |              0 |       1 | 2019-03-21 06:55:17 | 2019-03-21 12:55:18 |
+-------+---------+------------+--------------+-------------------+---------------+---------------+---------------+----------------+---------+---------------------+---------------------+
1 row in set (0.00 sec)

更新: 在 MySQL CLI 中查看会出现警告:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into contacts ( user_id, contact_name ) values (21, 'Some Person' );
Query OK, 1 row affected (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------+
| Level   | Code | Message                                                       |
+---------+------+---------------------------------------------------------------+
| Warning | 1196 | Some non-transactional changed tables couldn't be rolled back |
+---------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)

是什么让一些表成为非事务性的?

最佳答案

alerts 表似乎正在使用 MyISAM 引擎,does not support transactions :

mysql> show table status like 'alerts';
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name   | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation       | Checksum | Create_options | Comment |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| alerts | MyISAM |      10 | Dynamic    |   18 |            136 |        2712 | 281474976710655 |         2048 |       256 |          48690 | 2019-03-19 10:38:39 | 2019-03-21 06:55:19 | NULL       | utf8_general_ci |     NULL |                |         |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.00 sec)

要更改数据库引擎,请遵循指南 here和:

mysql> ALTER TABLE 警报 ENGINE=InnoDB;

关于MySQL ROLLBACK 不删除新记录-sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55281055/

相关文章:

php - 如何在一个查询中使用这两个选择查询

node.js - 使用 Node.js 和 fetch,当站点始终返回 response.ok = true 时,如何确定站点的 HTTP 版本是否处于事件状态?

node.js - LOCK 在sequelize -> types/transaction.d.ts 中定义为ENUM 和接口(interface)

node.js - Sequelize Many-to-Many Association -- "Include"查询错误说没有关联

javascript - FindAll 内部模型 Hook 'afterFind' 在 nodejs 中使用 sequelize 不起作用

java - Hibernate @OneToMany @ManyToOne 插入多个表

php - mysqli_fetch 循环不工作

mysql - 使用 maxdate 更新行,其中给出了 id from select 和 maxdate from select

javascript - Node、Webpack-Dev-Server、React 的开发模式 - 嵌套路由没有此类文件或目录

node.js - 我应该在哪里保存客户端的 JWT 以供将来请求?