Hello Django lovers,
I need to separate my application data into multiple Postgresql schemas. I’m trying to have a simple solution for that. So we have a new database backend which was inherited from the Django PostgreSQL backend _cursor
is replaced to be able to add SET search_path=[schema_name]
to queries. here is a sample code:
from django.db.backends.postgresql import base
class DatabaseWrapper(base.DatabaseWrapper):
def _cursor(self, name=None):
cursor = super()._cursor(name=name)
cursor.execute(f'SET search_path={self._get_schema()}')
return cursor
- for some operation like add or read in Django admin i see this error:
File "/b/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.ProgrammingError: syntax error at or near "SET"
LINE 1: ...5572462592_sync_1" NO SCROLL CURSOR WITH HOLD FOR SET search...
I have tried with DISABLE_SERVER_SIDE_CURSORS
but the Django admin terminates my session and returns to the login page
-
i think modifying
_cursor
method is not a professional way because I have found someN+1 Query
warnings in Sentry -
this method is working with DRF very well, and I can distribute my data using API but seems modifying
_cursor
method, is not an excellent way for Django admin. for example, most of the time Django admin redirects me to the login page without any error and message
if you have any idea, i really like to hear that.