title: Python SQL Alchemy Multiple Databases - Binding Automap_Base
body: <p>I am working through SQL Alchemy but struggling with how to structure the information from the docs into my project.  I have two databases, my first will be used to store all new information from the python application.  Where as the second database (DB1 in this case) is an existing database that I need to access information from. What is the right way to create this structure using SQLAlchemy?</p>
  
  <p>I used the suggested BINDS method for multiple databases.  This seems to be working.</p>
  
  <pre><code>class BaseConfig(object):
       SECRET_KEY = "SO_SECURE"
       DEBUG = True
       SQLALCHEMY_DATABASE_URI = 'mssql+pyodbc://sa:funpassword@(localdb)\MSSQLLocalDB/Testing?driver=SQL+Server+Native+Client+11.0'
       SQLALCHEMY_BINDS = {
           'DB1': 'mssql+pyodbc://sa:$funpassword@ProdDB/DB1?driver=SQL+Server+Native+Client+11.0'
       }
       SQLALCHEMY_TRACK_MODIFICATIONS = True
  </code></pre>
  
  <p>This configuration seems to work okay because I am able to create new models in both of these databases using the code below.  (This was done to just confirm that I was able to connect to both). db is my SqlAlchemy(app) initialization in my index.py file.</p>
  
  <pre><code>from index import app, db
  
  #Test Writing To Default DB
  class Test(db.Model):
       id = db.Column(db.Integer(), primary_key=True)
       employeeNum = db.Column(db.String(255), unique=False)
       job = db.Column(db.String(255))
  
      def __init__(self, employeeNum, job):
           self.employeeNum = employeeNum
           self.job = job
  
  # Test Writing To DB1
  class Test(db.Model):
       __bind_key__ = 'DB1' 
       id = db.Column(db.Integer(), primary_key=True)
       employeeNum = db.Column(db.String(255), unique=False)
       job = db.Column(db.String(255))
  
      def __init__(self, employeeNum, job):
           self.employeeNum = employeeNum
           self.job = job
  </code></pre>
  
  <p>I have tried many combinations using the table and automap_base from SQLAlchemy docs but this does not seem to work.  I am not sure how I can use the bind_key of DB1 when I am trying to map existing tables.</p>
  
  <pre><code>from index import app, db
  
  def loadSession():
      Table('employee', db.metadata, Column('emp_num', Text, primary_key=True))
      Base = automap_base(metadata=metadata)
      Base.prepare()
      employee = Base.classes.employee
      emp = db.session.query(employee).all()
      for i in emp:
          print(i.data)
  </code></pre>
  
  <p>Thanks for any help you can provide. </p>