node-sqlite3 icon indicating copy to clipboard operation
node-sqlite3 copied to clipboard

this.lastID is set when conditional insert fails

Open e1sbaer opened this issue 6 years ago • 1 comments

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
    }
  }

);

e1sbaer avatar Oct 09 '19 21:10 e1sbaer