title: Flask-SQLAlchemy - how do sessions work with multiple databases?
body: <p>I'm working on a Flask project and I am using Flask-SQLAlchemy.<br>
  I need to work with multiple already existing databases.<br>
  I created the &quot;app&quot; object and the SQLAlchemy one:<br></p>
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  
  
  app = Flask(__name__)
  db = SQLAlchemy(app)
  </code></pre>
  <p>In the configuration I set the default connection and the additional binds:<br></p>
  <pre><code>SQLALCHEMY_DATABASE_URI = 'postgresql://pg_user:pg_pwd@pg_server/pg_db'
  SQLALCHEMY_BINDS = {
      'oracle_bind': 'oracle://oracle_user:oracle_pwd@oracle_server/oracle_schema',
      'mssql_bind': 'mssql+pyodbc://msssql_user:mssql_pwd@mssql_server/mssql_schema?driver=FreeTDS'
  }
  </code></pre>
  <p>Then I created the table models using the declarative system and, where needed, I set the
  <code>__bind_key__</code> parameter to indicate in which database the table is located.<br>
  For example:<br></p>
  <pre><code>class MyTable(db.Model):
      __bind_key__ = 'mssql_bind'
      __tablename__ = 'my_table'
      
      id = db.Column(db.Integer, nullable=False, primary_key=True)
      val = db.Column(db.String(50), nullable=False)
  </code></pre>
  <p>in this way everything works correctly, when I do a query it is made on the right database.<br>
  <br>
  Reading the SQLAlchemy documentation and the Flask-SQLALchemy documentation I understand these things
  (i write them down to check I understand correctly):<br></p>
  <ul>
  <li>You can handle the transactions through the session.</li>
  <li>In SQLAlchemy you can bind a session with a specific engine.</li>
  <li>Flask-SQLAlchemy automatically creates the session (scoped_session) at the request start and it destroys it at the request end<br></li>
  </ul>
  <p>so I can do:<br></p>
  <pre><code>record = MyTable(1, 'some text')
  db.session.add(record)
  db.session.commit()
  </code></pre>
  <p>I can not understand what happens when we use multiple databases, regarding the session, in Flask-SqlAlchemy.</p>
  <p>I verified that the system is able to bind the table correctly at the right database through the <code>__bind_key__</code> parameter,
  I can, therefore, insert data on different databases through <code>db.session</code> and, at the commit, everything is saved.<br>
  <br>
  I can't, however, understand if Flask-SQLAlchemy create multiple sessions (one for each engine) or if manages the thing in a different way.<br>
  In both cases, how is it possible refer to the session/transaction of a specific database?<br>
  If I use <code>db.session.commit()</code> the system does the commit on all involved databases, but how can I do if I want to commit only for a single database?<br>
  I would do something like:<br></p>
  <pre><code>db.session('mssql_bind').commit()
  </code></pre>
  <p>but I can not figure out how to do this.<br>
  <br>
  I also saw a Flask-SQLAlchemy implementation which should ease the management of these situations:<br>
  <br>
  Issue: <a href="https://github.com/mitsuhiko/flask-sqlalchemy/issues/107" rel="noreferrer">https://github.com/mitsuhiko/flask-sqlalchemy/issues/107</a><br>
  Implementation: <a href="https://github.com/mitsuhiko/flask-sqlalchemy/pull/249" rel="noreferrer">https://github.com/mitsuhiko/flask-sqlalchemy/pull/249</a><br>
  <br>
  but I can not figure out how to use it.<br>
  <br>
  In Flask-SQLAlchemy how can I manage sessions specifically for each single engine?</p>