How to get data from multiple database using sqlalchemy?

Viewed 37

I try to write the application which need to access to multiple database for get the data,
and this is what i have try.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://flask:password@localhost/one'
app.config['SQLALCHEMY_BINDS'] = {'two': 'postgresql://flask:password@localhost/two',
                                'three':'postgresql://flask:=password@localhost/three'}
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 
db = SQLAlchemy(app)

# This is the table that i want to get data which in database three
class Roles(db.Model):
    __tablename__ = 'roles'
    __bind_key__ = 'three'
    role_code = db.Column(db.String(30), primary_key=True)
    role_name = db.Column(db.String(30))
print(Roles.query.all())

When i have run the application but it occur the error like this.

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from application import app
  File "/Users/ykn/Work/Project/GitHub/Service/application/__init__.py", line 14, in <module>
    from application import routes
  File "/Users/ykn/Work/Project/GitHub/Service/application/routes.py", line 26, in <module>
    print(Roles.query.all())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3246, in all
    return list(self)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3405, in __iter__
    return self._execute_and_instances(context)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3430, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 984, in execute
    return meth(self, multiparams, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_clauseelement
    ret = self._execute_context(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1287, in _execute_context
    self._handle_dbapi_exception(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1481, in _handle_dbapi_exception
    util.raise_(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1247, in _execute_context
    self.dialect.do_execute(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InsufficientPrivilege) permission denied for table roles

[SQL: SELECT roles.role_code AS roles_role_code, roles.role_name AS roles_role_name, FROM roles]
(Background on this error at: http://sqlalche.me/e/f405)

So what is a problem and how can i get the data from multiple database?

0 Answers
Related