I want to get a row from mysql database, then modiy some fields and commit. Before I commit, updating this row shoud be forbidden. so I use with_lockmode(‘update’). I use the
following RoutingSession and I find it use Slave to do this query. But I want to do this query using Master, what should I do in get_bind???
class RoutingSession(Session):
def get_bind(self, mapper=None, clause=None):
if self._flushing:
return engines['master']
else:
return engines['slave']
row = session.query(SomeTable).filter(SomeTable.id = 1).with_lockmode('update').one
row.somefield = 'newcontent'
session.commit()
I’m assuming you got this recipe from my blog post at Django-style Database Routers in SQLAlchemy. If you look in the section “manual access” you’ll see a recipe for when you need to explicitly point the routing session to a certain node:
Then you just do your operation with that bind explicitly: