【问题标题】:Knex does not return insert IdKnex 不返回插入 ID
【发布时间】:2017-09-13 21:14:24
【问题描述】:

这是我的表(CELLID)结构。

+---------+------------+------+-----+---------+-------+
| Field   | Type       | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| CELL_ID | int(11)    | NO   | PRI | NULL    |       |
| STATUS  | tinyint(4) | NO   |     | NULL    |       |
+---------+------------+------+-----+---------+-------+

这是我要插入表格的代码。

knex('CELLID').insert(insertObj)
    .then(function (result) {
      _log.info(reqContainer.uuid, "Successfully Added To CELLID||", result)
      // respond back to request
      _log.info(reqContainer.uuid, "Exiting CELLID_S");
      return resolve(result)  // respond back to request
    })
    .catch(function (err) {

      _log.error(reqContainer.uuid, "Failed Adding To CELLID ||", err)
      _log.error(reqContainer.uuid, "Exiting CELLID_S");
      // respond back to request
      return reject(Error("Failed Adding CELLID"));
    })

成功插入后,必须返回 ID。在我的情况下不会发生这种情况。我总是在插入时得到 ID 为 0。

我曾尝试添加一个额外的列,自动递增主键 ID(将 CELL_ID 删除为 PK)。在这种情况下,我得到了 ID(自动递增值)。

我在这里错过了什么?

谢谢。

【问题讨论】:

  • 返回 0 id 时,您可以检查实际插入数据库的内容吗?如果将 id 列的名称更改为 id而不是 CELL_ID,是否有帮助?
  • 不,如果我将 id 列的名称更改为 id 而不是 CELL_ID 也没关系。我想我们得到插入的 id ,只有当我们的 id 是自动生成(自动增量)。
  • 同样的问题,不知道为什么:(
  • .returning() is not supported by mysql and will not have any effect. 可能是因为这个
  • .returning() 不受 sqlite3 支持,不会有任何效果。

标签: mysql node.js knex.js


【解决方案1】:

我就是这样用的,(提示:一定要处理插入失败)

return knex(TABLE.CAPTCHA).insert({
    ...captcha,
    expire_time: createExpireTime()
}).then(ids => {
    return ids.length ? ids[0] : false;
});

【讨论】:

    【解决方案2】:

    您可能还需要在此处发布 insertObj,因为您已经设法创建了一个禁止 null 和默认 null 的列 STATUS。

    对于其他想知道返回()的人:问题是,MySQL 中没有实现特定的“返回”功能 - 如果您使用 returning(),knex 会抱怨的原因。但是如果你写一个回调,knex 本身可以返回插入行的 ID。

    knex.insert({
     name: 'Alice',
    })
    .into('person')
    .then(function (id) {
      console.log(id) // [1001]
    });
    

    【讨论】:

      【解决方案3】:

      这是我用的

      knex(tableName).insert(data).then(row => {return row[0]});
      

      【讨论】:

        【解决方案4】:

        要返回刚刚插入的记录的 id,请使用以下语法。

        addCarMake: function(model, callback) {
            return db.insert(model).into('car_make').returning("make_id").then(function (id) {
                console.log("make_id====" + JSON.stringify(id));
                id2: Number = id;
                // callback(null,id);
                return db('car_make').where('make_id', id[0]).first();
                // return db.raw('select * from car_make where make_id=?',id)
            });
        }
        

        但返回的 id 是数组形式,所以总是使用 id[0] 来进一步处理它。

        【讨论】:

          【解决方案5】:

          MySQL 不支持returning,因此无法通过 Knex 获得。

          这是来自 Knex 文档的受支持数据库列表:http://knexjs.org/#Builder-returning

          这是我知道的获取最后插入行的唯一其他方法:How to get the ID of INSERTed row in mysql?

          【讨论】:

          • returning 对于 MySQL 返回 id。 (似乎只有id 没有别的)。
          • @Dan 我相信即使不使用returning,它也会返回id,或者至少这就是它与我们的MySQL应用程序的工作方式,我们没有使用returning。但我也只是假设 OP 正在使用基于 mysql 标签的 mysql
          【解决方案6】:

          您必须将第二个参数传递给 insert 方法,该方法指定要从中检索值的列。

          在您的情况下,它应该如下所示:

          knex('CELLID').insert(insertObj, 'CELL_ID')
              .then(function (result) {
                  // ...
              })
          

          【讨论】:

          • 这适用于 mysql 吗?对于多插入。我知道 mysql 不支持它。但是在 mysql 中我们可以使用 LAST_INSERTED_ID() 但它不能给我们所有的 id。过早地我们可以认为我们可以通过最后一个 id 推断出其余的 id。但是是否也可以插入其他行。多循环插入是否因此排队和处理。或者他们不是。我认为他们不是。所以我们不能假设任何事情。 & 获取 id 的唯一方法可能是重新查询它们,我们需要有一种方法来获取它们。或者一次插入一个。 &mysql可以处理。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多