在Flask-SQLAlchemy Openshift中将对象添加到多个数据库

时间:2014-09-26 14:41:57

标签: python mysql sqlalchemy openshift flask-sqlalchemy

我试图在Openshift上托管的烧瓶应用程序中使用SQLAlchemy将两个单独的对象添加到两个数据库中。我有两个数据库通过绑定使用

连接
application.config['SQLALCHEMY_BINDS']={'rfps': os.environ.get('OPENSHIFT_MYSQL_DB_URL')}
application.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL') 
db = SQLAlchemy(application) 

和两个班级

class ExampleOne(db.Model):
    id = db.Column(db.String, primary_key=True)
    category = db.Column(db.String, nullable=True)
    title = db.Column(db.String, nullable = True)
    size = db.Column(db.String, nullable=True)
    def __init__(self, category = None, title = None, size = None):
        self.category = category
        self.title = title
        self.size = size

class ExampleTwo(db.Model):
    __bind_key__ = 'rfps'
    id = db.Column(db.String, primary_key=True)
    category = db.Column(db.String, nullable=True)
    title = db.Column(db.String, nullable = True)
    size = db.Column(db.String, nullable=True)
    def __init__(self, category = None, title = None, size = None):
        self.category = category
        self.title = title
        self.size = size

出于测试目的,我在每次部署期间创建并删除所有表

def dbinit():
    db.drop_all()
    db.create_all()
    db.session.add(ExampleOne(category="This part Works", title="This part works", size = "This part works"))
    db.session.add(ExampleTwo(category="This part does not", title="This part does not", size = "This part does not"))
    db.session.commit()

然后

if __name__ == '__main__':
    dbinit()
    application.run(debug=True, host="0.0.0.0", port=8888)

目前我只能写入Postgresql数据库。关于如何成功将对象提交到MySQL数据库的任何建议都将是绝对的救命。感谢。

0 个答案:

没有答案