Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get engine associated with SQLAlchemy Object

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.

like image 648
Swetabh Avatar asked Sep 18 '15 07:09

Swetabh


1 Answers

Use db.get_engine(bind='my_bind_key'). db.engine is just a shortcut to this for the default bind. 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.

like image 195
davidism Avatar answered Nov 14 '22 23:11

davidism