django database routing with transactions

亡梦爱人 提交于 2019-12-23 09:31:45

问题


Referring to the example in Django documentation for multiple databases in one application,

https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example

" It also doesn’t consider the interaction of transactions with the database utilization strategy. "

How do I handle the interaction stated above.

The scenario is this:

I am using postgresql as my database. I have setup up a replica and want all the reads to "auth" tables to go to replica. Following the documentation I wrote a database router. Now whenever I try to log in to my application is throws the following error.

DatabaseError: cannot execute UPDATE in a read-only transaction.

This happens when Django tries to save the "last_login" time. Because, in the same view it first fetches the record from replica, and then tries to update the last_login time. Since it happens in one transaction so the same database is used, i.e. replica.

How do I handle this?

Thoughts?

来源:https://stackoverflow.com/questions/17169971/django-database-routing-with-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!