Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
How exactly does it not work for you? Error? The code you show looks good and should actually work, unless there is something wrong with the configuration. –  van Sep 26 '14 at 15:23
    
The application doesn't load right when I push it trying to add the ExampleTwo object, and returns a 500 error. The logs end at waiting for a MySQL request. –  Brandon Bass Sep 26 '14 at 17:56
    
I have also tried to write to two different tables within the postgresql database, by adding __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
    
So it is not the problem of using multiple databases. Is your example really representative of what you are testing in reality? –  van Sep 26 '14 at 18:51
    
@BrandonBass I'm facing the same 500 error but just with a single db(mysql), maybe is not well configured? –  Ricardo Dec 27 '14 at 11:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.