cannot execute SELECT FOR UPDATE in a read-only transaction
I have a MASTER/SLAVE setup of Postgres MASTER : To write data SLAVE : To read data While trying to make PATCH request with POSTMAN I am getting following error: {"error": "('cannot execute SELECT FOR UPDATE in a read-only transaction\n',)"}
I feel for PATCH request db_for_read is being called. Is there a way to fix it.
@tripsus the best way to help us fix that is to isolate the query that is causing the issue.
Not going to accept this in its current state. If you want us to help you we need to be in a position to assess and replicate the issue.
@tomchristie @xordoquy I am using Jango rest framework for making all the request to db. All the standard request like GET, POST , DELETE is being handled by rest framework itself. My models.py class code is
from django.db import models
from common.models import VersionedModel
from knowlus.models import KnowlusDetails
class Circle(VersionedModel):
name = models.CharField(max_length=50, unique=True, null=False)
region = models.CharField(max_length=50)
std_code = models.CharField(max_length=10, db_index=True)
country_code = models.CharField(max_length=10, db_index=True)
created_time = models.DateTimeField('created_time')
def __unicode__(self):
return str(self.id) + "->" + self.name
class Meta:
unique_together = ('std_code', 'country_code',)
class OldCircleKnowlusMapping(models.Model):
new_circle = models.ForeignKey(Circle)
old_circle_id = models.IntegerField()
knowlus = models.ForeignKey(KnowlusDetails)
P.S. : Please let me know what other information do you need.
Most likely whatever software you're using to switch between the primary and replica is incorrectly selecting the replica DB for PATCH requests. I've no idea what you're using to select between those two databases - it could be being handled in middleware, or in the get_queryset in the view, but either way around there's no reason for me to think that it's got anything to do with django-rest-framework itself.
I am using DJango's Database routers to select database for read and write queries and using db_for_write and db_for_read method to select db. I followed this tutorial : https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example
Below is the code of database router:
author = 'monster' class CommonDBRouter(object):
def db_for_write(self, model, **hints):
print( "MonsteR :: db for write")
print (model)
return 'default'
def db_for_read(self, model, **hints):
print("MonsteR :: db for read")
print (model)
return 'slave'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
if obj1._meta.app_label == 'auth' or \
obj2._meta.app_label == 'auth':
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if app_label == 'auth':
return db == 'auth_db'
return None
Do you debugging statements there give you any info on which database is being selected for the operation?
Yes I used print statement to print logs in console. Below is the log for one PATCH request. MonsteR :: db for read MonsteR :: db for read MonsteR :: db for write MonsteR :: db for read MonsteR :: db for read
I am not sure why this is called so many times for one request.
This isn't really suitable for raising as an issue on Django's ticket tracker unless it can be broken down into the simplest possible reproducible example. However it's not impossible that there's a valid issue in Django here.
Personally I'd stop using the automatic database routing, and instead override get_queryset to add .using(db=...) and select the appropriate database, based on the incoming request method.
@tripsus this looks like a django related issue.
Well reporduction of this isssue is really simple. As I have mentioned above.
- Create a Master(Write access)/Slave(read access) db server.
- Use database routers to select between master and slave by defining db_for_read & db_for_write.
- Send a Patch rest request.
@xordoquy Yes while having this conversation I also realized that this is DJango issue rather than rest framework issue. @tomchristie It is not possible to use .using in my case because we have lots of application running at this time and also we have not overridden any methods of Model class.
Thanks a lot for looking into it.
Where there's scope for breaking this down is in reducing this to a much simpler view.
Apologies for not being more helpful here - it's hard to know how best to tackle, it may be that there's a valid Django issue here, but is it worth our time in breaking this down to the simplest possible case and determining that, or should we be pushing that burden onto you.
The answer to that would be more clear if it was demonstrably a REST framework issue (which it may still be).
If you were able to take some time to break this down into a simpler case (eg make the view simpler and simpler until you have the most minimal possible example) I'd be willing to then act on that.