Databases in deployments?

Question:

Trying to use Replit Database in a deployment but getting 'NoneType' object does not support item assignment Are these available for use in deployments? It works fine when just running the Replit.

Replit Profile: https://replit.com/@CarloDiCelico1

Can you send the full text of the error?

Sure, thanks. I think the db object from from replit import db isn’t getting instantiated? I’m not sure why this would work when running from console but not when running in a deployment, though.

2023-07-18 13:33:02 - 'NoneType' object does not support item assignment
Traceback (most recent call last):
File "/home/runner/3b32ffbd-dee3-4af0-9ba9-b3c2fb857b1f/venv/lib/python3.10/site-packages/chainlit/utils.py", line 36, in wrapper
return await user_function(**params_values)
File "/home/runner/3b32ffbd-dee3-4af0-9ba9-b3c2fb857b1f/main.py", line 127, in process_response
db[timestamp] = res
TypeError: 'NoneType' object does not support item assignment

Welcome to Ask! Looking at your Replit profile, you appear to have no Repls. I assume this is a private Repl. Could you either make it public (preferred) or provide a code snippet?

How to use ReplDB (just in case this solves your problem)
from replit import db
db["my_db"] = {}
db["my_db"].update({"name":"bob"})
# You can just keep making db's and
# adding to them
# IIRC it supports lists too

Hi! Thanks! Yes, this is exactly what I’m doing. The line db[timestamp] = res works when running the Replit but not in deployment.

1 Like

Are you able to provide us with your Repl (made public) or at least a code snippet?

Deployments do not have persistent filesystems so anything written to the deployment while it’s running will go away if it is re-run (if it can be written to it at all). However, my understanding is that ReplDB is meant to mitigate this issue, so that you can save data to a database instead of your filesystem. So this may be a bug. Still your code would be helpful.

3 Likes

I’m troubleshooting & will share my findings, but my working hypothesis is that db is None when I import it in the deployment environment only for some reason.

1 Like

Confirmed! Doing:

from replit import db
logging.info(f'db is {db}')

will show db is None in your Deployment logs, but when running the Replit via Run, will show the db info 2023-07-18 23:28:40 - db is <Database(db_url='https://kv.replit.com/v0/...

1 Like

I wonder if the ReplitDB is meant to be available in Deployments in the first place? :thinking:

It isn’t as far as I know.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.