tursodatabase / tursodatabase/libsql-js

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

Open
#95 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
335
Forks
48
Avg merge
3d 2h
Merged PRs (30d)
3

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided JavaScript reproduction using defaultSafeIntegers(true), stmt.run(), and stmt.all(), then inspect the existing tests for BigInt and SafeIntegers coverage. Done means stmt.run().lastInsertRowid matches the inserted rowid, with the expected bigint type and full precision for RANDOM ROWID.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, sqlite
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.