tursodatabase / libsql-experimental-python

libSQL API for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is `.sync` required to read my writes?

avinassh opened this issue · comments

Consider the following example:

import os

import libsql_experimental as libsql

print(F"syncing with {os.getenv('LIBSQL_URL')}")
connection = libsql.connect("hello.db", sync_url=os.getenv("LIBSQL_URL"), auth_token=os.getenv("LIBSQL_AUTH_TOKEN"))
connection.sync()

connection.execute("CREATE TABLE my_table(rowid INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT);")
connection.commit()
# connection.sync()

max_id = connection.execute(f"SELECT max(rowid) as rowid FROM my_table").fetchone()[0]
print("max_id is", max_id)

This fails with following error:

Traceback (most recent call last):
  File "writes_no_sync.py", line 13, in <module>
    max_id = connection.execute(f"SELECT max(rowid) as rowid FROM my_table").fetchone()[0]
ValueError: no such table: my_table

The table is created and I can see it in the shell. If I uncomment the connection.sync() line, it works as expected.

Is the connection behaves like a replica and since I am reading my own writes, I need to sync?

Yes, a sync() is indeed require to read your writes right now, but we will fix that in tursodatabase/libsql#623