Django transaction database routing

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

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

"He also does not consider the interaction of transactions with the strategy of using the database."

How to handle the interaction above.

The scenario is this:

I am using postgresql as my database. I have set up a replica and want all reads in the "auth" tables to go to the replica. After the documentation, I wrote a database router. Now, when I try to enter my application, the following error is thrown.

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

This happens when Django tries to save the last_login time. Since in the same view it first retrieves the record from the replica and then tries to update the last_login time. Since this happens in a single transaction, therefore, the same database is used, i.e. a replica.

How can I do it?

Thoughts?

+4
source share

Source: https://habr.com/ru/post/1486824/


All Articles