tursodatabase / tursodatabase/libsql

lastrowid API not working on libsql-server hosted databases

Open
#1,097 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug remote protocol
Dominant language
C
Stars
17.2k
Forks
531
Avg merge
1h 12m
Merged PRs (30d)
1

Description

The lastrowid API always returns a 0 from libsql-server databases.
For local file and in-memory databases it seems to work fine.
Embedded local databases with sync seem to be fine as well.

To test, I created a python script that uses the libsql-experimental-python package to test against the different connection types.
The script drops a table, creates a table, and then inserts twice, printing the lastrowid after each operation, to which I would expect the result [0, 0, 1, 2] for all the connection types.

import libsql_experimental as libsql
import os

LIBSQL_URL=os.environ.get("LIBSQL_URL")
LIBSQL_AUTH_TOKEN=os.environ.get("LIBSQL_AUTH_TOKEN")

def test_lastrowid(test_name, *args, **kwargs):
    row_ids = []
    connection = libsql.connect(*args, **kwargs)
    cursor = connection.cursor()
    cursor.execute("DROP TABLE IF EXISTS heroes")
    row_ids.append(cursor.lastrowid) # 0
    cursor.execute("CREATE TABLE heroes (id INTEGER PRIMARY KEY, hero_name TEXT)")
    row_ids.append(cursor.lastrowid) # 0
    cursor.execute("INSERT INTO heroes (hero_name) VALUES ('Batman')")
    row_ids.append(cursor.lastrowid) # 1
    cursor.execute("INSERT INTO heroes (hero_name) VALUES ('Superman')")
    row_ids.append(cursor.lastrowid) # 2
    print(f'{test_name}: {row_ids}')
    connection.commit()

test_lastrowid("Local file", "test.db")
test_lastrowid("In memory", ":memory:")
test_lastrowid("Local SQLD", database="http://127.0.0.1:8080")
test_lastrowid("Remote Turso", database=LIBSQL_URL, auth_token=LIBSQL_AUTH_TOKEN)
test_lastrowid("Embedded turso replica", "localsync.db", sync_url=LIBSQL_URL, auth_token=LIBSQL_AUTH_TOKEN)

Actual results are:

Local file: [0, 0, 1, 2]
In memory: [0, 0, 1, 2]
Local SQLD: [0, 0, 0, 0]
Remote Turso: [0, 0, 0, 0]
Embedded turso replica: [0, 0, 1, 2]

Contributor guide

Open the contributing guide

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 by running the supplied Python reproduction with libsql-experimental-python against the local SQLD and remote Turso connections, then compare those results with local and embedded connections. Trace the remote INSERT and cursor.lastrowid path; done means hosted databases return the inserted row IDs, producing [0, 0, 1, 2] consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlite
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.