libsql-js icon indicating copy to clipboard operation
libsql-js copied to clipboard

stmt.run().lastInsertRowid is wrong with RANDOM ROWID

Open ykuksenko opened this issue 1 year ago • 0 comments

Hi, I am new to JS so it is possible I am missing something.

  • I am using the RANDOM ROWID feature with libsql. When this is enabled I cannot retrieve items using the lastInsertRowid property from stmt.run() commands after insertion. The lastInsertRowid is slightly off from the actual rowid. However using stmt.all() after a select * ... statement I can get the correct rowids.
  • Looking at the tests they do not seem to cover the BigInt/SafeIntegers use case.

Here is example code to reproduce the issue:

import Database from 'libsql';

const db = new Database(':memory:');
db.defaultSafeIntegers(true);
db.exec(`
  DROP TABLE IF EXISTS users;
  CREATE TABLE users (rowid INTEGER PRIMARY KEY, name TEXT) RANDOM ROWID;`);

const stmt_insert = db.prepare("INSERT INTO users(name) VALUES (?)");
const info_insert = stmt_insert.run('alma');
console.log([typeof info_insert.lastInsertRowid, info_insert.lastInsertRowid]);
const stmt_list = db.prepare("SELECT * from users");
const info_list = stmt_list.all()
console.log([typeof info_list[0].rowid ,info_list]);

and sample output:

$ node ex.js
[ 'number', 4504423795271622000 ]
[ 'bigint', [ { rowid: 4504423795271622254n, name: 'alma' } ] ]

ykuksenko avatar Mar 28 '24 04:03 ykuksenko