sequelize的事务问题(已解决)
发布于 8 年前 作者 liuxufei 13767 次浏览 来自 问答

sequelize的事务好像只支持insert,不支持update啊。

2016/04/14 更新: 目前事务的效果: QQ图片20160414223831.png

2016/04/11 更新: 谢谢大家的解答,这几天忙另外的项目,没来得及看这里,对不住了。

数据库是 MySQL,引擎为 InnoDB 1、 开始事务、insert 和 commit 在console都是前面显示: Executing (a4b68a95-bd30-4f6f-b1f9-d5d8277aa2af)

而 update 则是: Executing (default)

2、 还有事务3那里是循环,好像这样处理有问题。

类似代码如下:

return sequelize.transaction(function (t) {

    // 事务 1
    return models.Table1.create({
        user_id: user_id,
        nickname: req.session.user.nickname,
        count: count
    }, {transaction: t}).then(function (table1) {

        // 事务 2
        return models.Table2.update({
            count: count
        }, {
            where: {
                id: user_id,
            }
        }, {transaction: t}).then(function (table2) {

            models.Tables3.findAll({
                where: {
                    amount: {
                        $gt: 0,
                    }
                }
            }).then(function (tables3) {
                for (let i = 0; i < tables3.length; i++) {

                    // 事务 3
                    models.Tables3.update({
                        count: count
                    }, {
                        where: {
                            user_id: tables3[i].user_id
                        }
                    }, {transaction: t});
                }
            });

            // 事务 4
            return models.Table4.update({
                count: count,
            }, {
                where: {
                    user_id: user_id
                }
            }, {transaction: t}).then(function (table4) {

            });
        });
    });
}).then(function (result) {

  // Transaction has been committed
  // result is whatever the result of the promise chain returned to the transaction callback

}).catch(function (err) {

  // Transaction has been rolled back
  // err is whatever rejected the promise chain returned to the transaction callback

});
7 回复

可以,我试过

mysql? 引擎是 myisam ? 符合以上两个条件则是数据库不支持,insert 支持也许只是错觉

把环境和代码发出来

研究了好久的sequelize还是无法解决,有懂得可以帮忙提示下吗?

回到顶部