@@ -184,11 +184,12 @@ def after_rollback(self, session):
184
184
class _SignallingSession (Session ):
185
185
186
186
def __init__ (self , db , autocommit = False , autoflush = False , ** options ):
187
- Session .__init__ (self , autocommit = autocommit , autoflush = autoflush ,
188
- extension = db .session_extensions ,
189
- bind = db .engine , ** options )
190
187
self .app = db .get_app ()
191
188
self ._model_changes = {}
189
+ Session .__init__ (self , autocommit = autocommit , autoflush = autoflush ,
190
+ extension = db .session_extensions ,
191
+ bind = db .engine ,
192
+ binds = db .get_binds (self .app ), ** options )
192
193
193
194
def get_bind (self , mapper , clause = None ):
194
195
# mapper is None if someone tries to just get a connection
@@ -756,6 +757,20 @@ def get_tables_for_bind(self, bind=None):
756
757
result .append (table )
757
758
return result
758
759
760
+ def get_binds (self , app = None ):
761
+ """Returns a dictionary with a table->engine mapping.
762
+
763
+ This is suitable for use of sessionmaker(binds=db.get_binds(app)).
764
+ """
765
+ app = self .get_app (app )
766
+ binds = [None ] + list (app .config .get ('SQLALCHEMY_BINDS' ) or ())
767
+ retval = {}
768
+ for bind in binds :
769
+ engine = self .get_engine (app , bind )
770
+ tables = self .get_tables_for_bind (bind )
771
+ retval .update (dict ((table , engine ) for table in tables ))
772
+ return retval
773
+
759
774
def _execute_for_all_tables (self , app , bind , operation ):
760
775
app = self .get_app (app )
761
776
0 commit comments