13

I have associated multiple engines with a SQLAlchemy Object. To use sessionmaker to create sessions I need to get the right engine. When I try db.engine (db is my SQLAlchemy object), I get the default engine. How do I get the other associated engines? I would prefer if I could give a key and get the right engine.

I used Flask's SQLALCHEMY_BINDS to associate more than one engine.

1 Answer 1

17

Use db.engines[bind_key] to get a specific engine. db.engine is equivalent to db.engines[None].

However, you should not need to set up sessions yourself, Flask-SQLAlchemy already has all the parts in place to use the correct bind when using db.session.


Prior to Flask-SQLAlchemy 3, use db.get_engine(bind=bind_key). db.get_engine() is equivalent to db.get_engine(bind=None).

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.