django database routing with transactions
Asked Answered
E

0

8

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?

Encrata answered 18/6, 2013 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.