Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask-sqlalchemy with dynamic database connections

I have one main db, in which each client's own db connection is stored. So every client works with 2 db: main and its own db, connection of which must be decided for each http call. How can I do this using flask-sqlalchemy extension, or may be purely in sqlalchemy?

like image 633
synergetic Avatar asked Oct 28 '11 00:10

synergetic


1 Answers

You can handle multiple sessions in Flask-SQLalchemy:

engine = create_engine(DATABASE_URI)
db_session = scoped_session(sessionmaker(autocommit=False,
                                     autoflush=False,
                                     bind=engine))
db_session.query...()

and

engine2 = create_engine(DATABASE_URI2)
db_session2 = scoped_session(sessionmaker(autocommit=False,
                                     autoflush=False,
                                     bind=engine2))

db_session2.query2...()

sharing the same codebase.

like image 127
manelvf Avatar answered Sep 20 '22 01:09

manelvf