This is my table(CELLID) structure.
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| CELL_ID | int(11) | NO | PRI | NULL | |
| STATUS | tinyint(4) | NO | | NULL | |
+---------+------------+------+-----+---------+-------+
And this is my code to insert into the table.
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"));
})
After a successful insert, the Id has to be returned.This does not happen in my case. I always get and Id of 0 on an insert.
I had tried by adding an extra column, auto-increment primary key ID(removing CELL_ID as PK).In this case, I get the ID(auto-increment value).
What am I missing here?
Thanks.
You have to pass second argument to the insert
method, which specifies a column to retrieve a value from.
In your case it should look like this:
knex('CELLID').insert(insertObj, 'CELL_ID')
.then(function (result) {
// ...
})
MySQL doesn't support returning
, so it's not available through Knex.
Here is a list of supported DB's from the Knex docs: http://knexjs.org/#Builder-returning
This is the only other method that I'm aware of to get the last inserted row: How to get the ID of INSERTed row in mysql?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With