Python replit DB not synced even after setting environ

Problem description:
I was attempting to make a simple send-or-receive system utilizing replit DB. However, using two different accounts on two different browsers I see that the DBs are not synced even after setting os.environ. I checked both to make sure they were on the same copy of the code yet no luck. This also occured to me (but I was oblivious) when I was creating a game with my friends and occasionally their accounts would be deleted or accounts would not be able to read other account’s DB values.

Expected behavior:
For the DBs to sync (and yes I tried rebooting the repl and refreshing page)

Actual behavior:
DBs fail to sync even after setting os.environ

Steps to reproduce:
Run the code

Bug appears at this link:
https://replit.com/@-jpg/dbtest#main.py
REPLIT_DB_URL is also at the link

Browser/OS/Device:
Firefox + Chrome/Windows 11/PC

Replit DB is a per-copy of the repl thing. This includes being run through the cover page. Unless you use a DB proxy or hardcode the URL to the DB, the cover page will not access the same one as your copy.

2 Likes

As I said, I made the database URL hard-coded.

1 Like

I don’t really know but this code worked for me:

from replit import Database

db = Database("https://kv.replit.com/v0/eyJhbGciOiJIUzUxMiIsImlzcyI6ImNvbm1hbiIsImtpZCI6InByb2Q6MSIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjb25tYW4iLCJleHAiOjE2ODA4NjAwMTAsImlhdCI6MTY4MDc0ODQxMCwiZGF0YWJhc2VfaWQiOiIxOGNhODQ0MS01Y2Y1LTU5OWUtYTVlNS01Y2M0MTQ1MWJjYWMiLCJ1c2VyIjoiYmlnbWluaWJvc3MiLCJzbHVnIjoiZkR2S1Jqb2lZSUYifQ.cweGDGKNAofKPD1WiI4QGebiNzaq_wnZsV1_wfKj_Cr3x50U-B9Cy3Xc7AwD3lhppTr_uaJzlKyExKJxrDf1yw")

db["reading"] = "hello world"
db["c"] = 0

repl link

To get your URL, enter this in shell: echo $REPLIT_DB_URL
Then, use the following Python code:

from replit import db
db.db_url = "put your db URL here"

Finally, use the DB as normal. To keep it secret, use an environmental secret to store the DB URL.
Please note that I learned all this from @Firepup650.

2 Likes

or

from replit import Database

Database("URL_HERE")
2 Likes

From reading your post, it sounds like you hardcode the URL into the Secrets tab, which is only accessible from your copy of the repl.

1 Like

i didn’t, but its working now