这是我的表(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(自动递增值)。
我在这里错过了什么?
谢谢。
最佳答案
您必须将第二个参数传递给 insert
方法,该方法指定要从中检索值的列。
在你的情况下它应该是这样的:
knex('CELLID').insert(insertObj, 'CELL_ID')
.then(function (result) {
// ...
})
关于mysql - Knex 不返回插入 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43465266/