【问题标题】:Knexjs: Transaction and raw queriesKnexjs:事务和原始查询
【发布时间】:2019-09-26 15:51:40
【问题描述】:

我们在 nodejs 中使用 knexjs。我们正在尝试将事务与原始查询一起使用,但它没有按预期工作。当抛出错误时,会调用回滚函数,但在数据库中我可以看到数据

  const trx = await knex.transaction();

  await trx.schema.createTable("test", function(table) {
    table.increments();
    table.string("name");
  });

  await trx("test").insert({ name: "foo1" });
  await trx("test").insert({ name: "foo2" });

  await trx.rollback();

是否可以将事务与原始查询一起使用?

【问题讨论】:

  • 您可以在事务中进行类似的查询吗?错误信息是什么?

标签: knex.js


【解决方案1】:

根据https://github.com/tgriesser/knex/issues/3452#issuecomment-534952063,创建表会导致隐式提交。因此,事务关闭,回滚无效。 一种可能的解决方法是将事务用于除创建表之外的所有语句。

您可以在下面看到我的 sn-p 根据此解决方法修改

await knex.schema.createTable("test", function(table) {
  table.increments();
  table.string("name");
});

const trx = await knex.transaction();

await trx("test").insert({ name: "foo1" });
await trx("test").insert({ name: "foo2" });

await trx.rollback();

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 2018-09-01
    • 1970-01-01
    • 2021-02-19
    • 2013-05-06
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多