5

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
  • Are these two databases sharing the same data? Commented Dec 19, 2011 at 23:10

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this code does not reflect how it would be done with Flask-SQLAlchemy, but rather specifically with SQLAlchemy directly.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.