Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - Configuring Flask-SQLAlchemy to use multiple databases with Flask-Restless

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.

According to the docs, configuring your models to use multiple databases with __bind_key__ seems pretty straightforward.

However it doesn't seem to be working for me.

I create my app and initialise my database like this:

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)

Then define my models including __bind_key__, which should tell SQLAlchemy which DB it needs to use:

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)
    ...

Then I fire up Flask-Restless like this:

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)

The app runs fine and when I hit http://localhost:5000/api/postgres_model/[id] 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).

Although when I hit http://localhost:5000/api/mysql_model/[id], I get a mysql_model_table does not exist error, indicating that it's looking in the Postgres DB, not the MySQL one.

What am I doing wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This was not working because of a simple typo:

__bind_key = 'db1'

Should have been

__bind_key__ = 'db1'

I've updated the original question and fixed the typo as an example of how this can work for others.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...