以下是我的配置,slave 是只读的:
engines = {
'master': create_engine(
settings.MASTER_URL, echo=settings.ECHO_SQL, pool_recycle=3600),
'slave': create_engine(
settings.SLAVE_URL, echo=settings.ECHO_SQL, pool_recycle=3600),
}
class RoutingSession(Session):
def get_bind(self, mapper=None, clause=None):
#return engines['master']
if self._flushing:
return engines['master']
else:
return engines['slave']
DBSession = scoped_session(sessionmaker(class_=RoutingSession))
当我做 :
s = DBSession()
#synchronize_session=False makes execution faster
s.query(Model).filter(Model.id == 1).delete(synchronize_session=False)
s.commit()
它引发了一个由于“只读”而无法删除的异常。
如何重写我的 RoutingSession 以使上述“删除”使用“主”数据库
---编辑1---
我做了更多的测试。上面的 'delete' 不同于 RoutingSession 将返回 'master' 的下面的 'delete':
model = s.query(Model).filter(Model.id == 1).first()
s.delete(model)
s.commit()