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?
1 Answer
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.
2 Comments
Michael Ekoka
this code does not reflect how it would be done with Flask-SQLAlchemy, but rather specifically with SQLAlchemy directly.
manelvf
Right, the code is only for plain SQLAlchemy, but question was not specific about using Flask-SQLAlchemy. Making it work with the wrapper should be easy, anyway. Also, you also can use both in the same codebase.