egg-sequelize 如何使用事务

Created on 8 May 2017  ·  17Comments  ·  Source: eggjs/egg

  • Node Version: 1.2.1
  • Egg Version: 7.9
  • Plugin Name: egg-sequelize
  • Plugin Version: 2.0.2

执行代码

const result = yield this.ctx.model.Sequelize.Transaction(function* (t) {
    const order = yield this.ctx.model.Orders.findAll({ transaction: t });
    const user = yield this.ctx.model.user.findAll({ transaction: t });
    return {
      order,
      user,
    };
}, this);

报错信息

Cannot read property 'QueryGenerator' of undefined
    at Function.Transaction (/Users/TDiDa/ChainService/node_modules/[email protected]@sequelize/lib/transaction.js:24:53)
    at Order.list (/Users/TDiDa/ChainService/app/service/order.js:42:53)
    at list.next (<anonymous>)
    at onFulfilled (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:65:19)
    at /Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:54:5
    at Object.co (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:50:10)
    at Object.toPromise (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:118:63)
    at next (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:99:29)
    at onFulfilled (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:69:7)
    at /Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:54:5
    at Object.co (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:50:10)
    at Object.toPromise (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:118:63)
    at next (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:99:29)
    at onFulfilled (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:69:7)
    at /Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:54:5
    at Object.co (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:50:10)

是不是我使用的方式错了,请问下如何使用

egg-sequelize document

Most helpful comment

正确的书写方式应该是

const t = yield this.ctx.model.transaction();
try {
  const order = yield this.ctx.model.Orders.findAll({ transaction: t });
  const user = yield this.ctx.model.user.findAll({ transaction: t });
  yield t.commit();
  return {
    order,
    user,
  };
} catch (err) {
  yield t.rollback();  
}

app.model 就是sequelize的实例。

All 17 comments

Transaction 应该不支持 generator function 吧,试试

const t = yield this.ctx.model.sequelize.transaction();
try {
  const order = yield this.ctx.model.Orders.findAll({ transaction: t });
  const user = yield this.ctx.model.user.findAll({ transaction: t });
  yield t.commit();
  return {
    order,
    user,
  };
} catch (err) {
  yield t.rollback();  
}

@huacnlee 事务要不要也在文档上写个例子?

@popomore 那个我还没用过,需要试试

我们也可以包一下 transaction 支持 generator function,这样就可以自动 commit 和 rollback 了。

@popomore 报错

Cannot read property 'dialect' of undefined
    at Function.Transaction (/Users/TDiDa/ChainService/node_modules/[email protected]@sequelize/lib/transaction.js:24:45)
    at Order.list (/Users/TDiDa/ChainService/app/service/order.js:42:48)
    at list.next (<anonymous>)
    at onFulfilled (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:65:19)
    at /Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:54:5
    at Object.co (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:50:10)
    at Object.toPromise (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:118:63)
    at next (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:99:29)
    at onFulfilled (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:69:7)
    at /Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:54:5
    at Object.co (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:50:10)
    at Object.toPromise (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:118:63)
    at next (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:99:29)
    at onFulfilled (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:69:7)
    at /Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:54:5
    at Object.co (/Users/TDiDa/ChainService/node_modules/[email protected]@co/index.js:50:10)

@TDiDa 你 find 方法带 Transaction 干嘛?

@huacnlee 只是写个例子

好像是小写,改了一下

还应该暴露 app.sequelize 实例

app.model 就是 Sequelize 的 instance 目前

@huacnlee 我说说 sequelize 本身的实例,不是 model 的实例 https://github.com/eggjs/egg-sequelize/blob/master/lib/loader.js#L31

现在要通过 instance 获取,model.xx.sequelize

我觉得暴露到 app 或 ctx 上没什么不好,放一个 Sequelize 类好像没啥用。

正确的书写方式应该是

const t = yield this.ctx.model.transaction();
try {
  const order = yield this.ctx.model.Orders.findAll({ transaction: t });
  const user = yield this.ctx.model.user.findAll({ transaction: t });
  yield t.commit();
  return {
    order,
    user,
  };
} catch (err) {
  yield t.rollback();  
}

app.model 就是sequelize的实例。

按照上面的方法使用事务会报这个错误: Cannot read property 'dialect' of undefined
请问怎么解决?

@shileiyuan sequelize配置项里有一个dialect: 'mysql',别落下

Was this page helpful?
0 / 5 - 0 ratings

Related issues

whlsxl picture whlsxl  ·  3Comments

skyyangpeng picture skyyangpeng  ·  3Comments

weijiatan456 picture weijiatan456  ·  3Comments

bupafengyu picture bupafengyu  ·  3Comments

lvgg3271 picture lvgg3271  ·  3Comments