title: Configuring Flask-SQLAlchemy to use multiple databases with Flask-Restless
body: <p>I have a Flask app that uses Flask-SQLAlchemy and I'm trying to configure it to use multiple databases with the Flask-Restless package.</p>
  <p>According to <a href="https://flask-sqlalchemy.palletsprojects.com/en/2.x/binds/" rel="nofollow noreferrer">the docs</a>, configuring your models to use multiple databases with <code>__bind_key__</code> seems pretty straightforward.</p>
  <p>However it doesn't seem to be working for me.</p>
  <p>I create my app and initialise my database like this:</p>
  <pre><code>from flask import Flask
  from flask.ext.sqlalchemy import SQLAlchemy
  
  SQLALCHEMY_DATABASE_URI = 'postgres://db_user:db_pw@localhost:5432/db_name'
  SQLALCHEMY_BINDS = {
      'db1': SQLALCHEMY_DATABASE_URI,
      'db2': 'mysql://db_user:db_pw@localhost:3306/db_name'
  }
  
  app = Flask(__name__)
  db = SQLALchemy(app)
  </code></pre>
  <p>Then define my models including <code>__bind_key__</code>, which should tell SQLAlchemy which DB it needs to use:</p>
  <pre><code>class PostgresModel(db.Model):
  
      __tablename__ = 'postgres_model_table'
      __bind_key__ = 'db1'
  
      id = db.Column(db.Integer, primary_key=True)
      ...
  
  
  class MySQLModel(db.Model):
  
      __tablename__ = 'mysql_model_table'
      __bind_key__ = 'db2'
  
      id = db.Column(db.Integer, primary_key=True)
      ...
  </code></pre>
  <p>Then I fire up Flask-Restless like this:</p>
  <pre><code>manager = restless.APIManager(app, flask_sqlalchemy_db=db)
  manager.init_app(app, db)
  
  auth_func = lambda: is_authenticated(app)
  
  manager.create_api(PostgresModel,
                     methods=['GET'],
                     collection_name='postgres_model',
                     authentication_required_for=['GET'],
                     authentication_function=auth_func)
  
  manager.create_api(MySQLModel,
                     methods=['GET'],
                     collection_name='mysql_model',
                     authentication_required_for=['GET'],
                     authentication_function=auth_func)
  </code></pre>
  <p>The app runs fine and when I hit <code>http://localhost:5000/api/postgres_model/[id]</code> I get the expected JSON response of the object from the Postgres DB (I'm guessing this is because I have it's credentials in SQLALCHEMY_DATABASE_URI).</p>
  <p>Although when I hit <code>http://localhost:5000/api/mysql_model/[id]</code>, I get a <code>mysql_model_table</code> does not exist error, indicating that it's looking in the Postgres DB, not the MySQL one.</p>
  <p>What am I doing wrong here?</p>