node-sqlite3
node-sqlite3 copied to clipboard
this.lastID is set when conditional insert fails
I have a conditional insert statement that only inserts a new row into table items if a row with matching ID exists in table lists. When executed with a list ID that is not found in the lists table and insertion fails the this.lastID is set to the ID of the last entry found in table lists.
var $listId = 'invalid list ID'
this.db.run(
'INSERT INTO items SELECT $listId AS listId, $text AS text WHERE EXISTS (SELECT 1 FROM lists WHERE id=$listId);',
{ $listId, $text },
function(error) {
if (error) {
// handle error
} else {
if (!this.lastID) {
// execution will never come to this point
// even when insertion of new item fails
// because 'this.lastID' is set to the highest
// rowId value found in table 'lists'
// handle failed insertion
}
// handle successful insertion
}
}
);