Home:ALL Converter>Explicit Master-Master DB setup with Flask and SQLAlchemy, hopefully with Flask-SQLAlchemy

Explicit Master-Master DB setup with Flask and SQLAlchemy, hopefully with Flask-SQLAlchemy

Ask Time:2013-10-30T00:01:15         Author:Oded

Json Formatter

I want to use an explicit master-master DB setup together with Flask and SQLAlchemy, hopefully this is supported with Flask-SQLAlchemy.

I want to be able to do something like the following code snippet but I'm not sure if it's supported by Flask-SQLAlchemy

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
SQLALCHEMY_DATABASE_URI = 'default_DB_uri'
SQLALCHEMY_BINDS = { 'master1':'first_master_DB_uri', 'master2': 'second_master_DB_uri' }

app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_BINDS'] = SQLALCHEMY_BINDS

db = SQLAlchemy(app)

@app.route('/some_endpoint')
def some_endpoint():
    # read some data for the default DB
    readData = db.session.query('select ...')

    m = SomeModel()
    masterSession1 = db.session(bind='master1')
    # persist the data in m into the first master
    masterSession1.add(m)

    masterSession2 = db.session(bind='master2')
    # persist the data into the second master
    masterSession2.add(m)

    return "some return value"

Is there a way to achieve this using Flask-SQLAlchemy and binds? I guess that Flask-SQLAlchemy already handles more than one engine with the binds but I can't see how to use that for an explicit DB selection and not a model based selection like mentioned here: http://pythonhosted.org/Flask-SQLAlchemy/binds.html

Thanks for the help.

Author:Oded,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19663404/explicit-master-master-db-setup-with-flask-and-sqlalchemy-hopefully-with-flask
yy