sqlalchemy declarative_base not found

hi, I’m running two free services, one maintains my code directly from GitHub and the other maintains the PostgreSQL database, but I’ve had problems when executing a post that saves data in the database, when testing both code and database work, even committing to the database server works, but not in the python environment that you bring up. I write the error below

Aug 30 10:52:16 PM  [2022-08-31 02:52:16,863] ERROR in app: Exception on /nuevo-cliente [POST]
Aug 30 10:52:16 PM  Traceback (most recent call last):
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/flask/app.py", line 2525, in wsgi_app
Aug 30 10:52:16 PM      response = self.full_dispatch_request()
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/flask/app.py", line 1822, in full_dispatch_request
Aug 30 10:52:16 PM      rv = self.handle_user_exception(e)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/flask/app.py", line 1820, in full_dispatch_request
Aug 30 10:52:16 PM      rv = self.dispatch_request()
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/flask/app.py", line 1796, in dispatch_request
Aug 30 10:52:16 PM      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
Aug 30 10:52:16 PM    File "/opt/render/project/src/app/views/views.py", line 53, in n_cliente
Aug 30 10:52:16 PM      db_context.commit()
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1451, in commit
Aug 30 10:52:16 PM      self._transaction.commit(_to_root=self.future)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 829, in commit
Aug 30 10:52:16 PM      self._prepare_impl()
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 808, in _prepare_impl
Aug 30 10:52:16 PM      self.session.flush()
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3383, in flush
Aug 30 10:52:16 PM      self._flush(objects)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3523, in _flush
Aug 30 10:52:16 PM      transaction.rollback(_capture_exception=True)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
Aug 30 10:52:16 PM      compat.raise_(
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
Aug 30 10:52:16 PM      raise exception
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3483, in _flush
Aug 30 10:52:16 PM      flush_context.execute()
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
Aug 30 10:52:16 PM      rec.execute(self)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 630, in execute
Aug 30 10:52:16 PM      util.preloaded.orm_persistence.save_obj(
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 212, in save_obj
Aug 30 10:52:16 PM      for (
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 373, in _organize_states_for_save
Aug 30 10:52:16 PM      for state, dict_, mapper, connection in _connections_for_states(
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1737, in _connections_for_states
Aug 30 10:52:16 PM      connection = uowtransaction.transaction.connection(base_mapper)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 625, in connection
Aug 30 10:52:16 PM      bind = self.session.get_bind(bindkey, **kwargs)
Aug 30 10:52:16 PM    File "/opt/render/project/src/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2147, in get_bind
Aug 30 10:52:16 PM      raise sa_exc.UnboundExecutionError(
Aug 30 10:52:16 PM  sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper mapped class NRcliente->NR_cliente, SQL expression or this Session.

I would appreciate a little help.

Hi Juan,

This appears to be an application level issue and while we can’t provide too much support for these types of issues I did find this on SO https://stackoverflow.com/questions/18445772/sqlalchemy-exc-unboundexecutionerror-could-not-locate-a-bind-configured-on-mapp which might be helpful.

1 Like

Hello, I have verified that answer before without success, searching the internet, I have found information that talks about the drivers that handle the pgsql connection, I really don’t know, but I have tried in other application containers and I have the same problem, maybe the development environment in the cloud has something different that does not allow it to function normally

Double check the environment variable, SQAlchemy create engine requires postgresql while Render provides DB URL with prefix postgres.

SQLAlchemy docs

1 Like

Hello, I modify the connection string, but the same error continues, I changed postgres to postgresql, I don’t know if it was what you told me, or do I have to create an environment variable in configuration that points postgresql to postgres?
thanks.

Hi Juan,

Have you been able to replicate this issue using your Python application with a local Postgres instance? Also these sqlalchemy docs might help simplify your connection logic.