I am trying to add two separate objects to two databases using SQLAlchemy in a flask application hosted on Openshift. I have two databases joined by a bind using
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)
and two classes
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
and
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
For test purposes, I'm creating and dropping all tables during each deployment as so
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()
then
if __name__ == '__main__':
dbinit()
application.run(debug=True, host="0.0.0.0", port=8888)
Currently I'm only able to write to the Postgresql database. Any suggestions on how I could successfully commit an object to the MySQL database would be absolutely lifesaving. Thanks.
__tablename__= "tableOne"
and__tablename__="tableTwo"
to each respective class definition, and removing the__bind_key__
attribute from the ExampleTwo() class. This still results in an error. – Brandon Bass Sep 26 '14 at 18:00