Hi all, I am currently experiencing strange issues that I don’t know how to troubleshoot anymore, so I came here to ask for help
I am running a setup with a primary database used for all my models and a secondary database that I want to use to query data from postgres views in a read-only manner. Therefore, I created an unmanaged model as for other views previously and added the attributes I want to query from the view to the model. Furthermore, I’ve added the model to the db_router that I have used before for different models as well. I expected this to work, but for some reason, my model does not return any results and behaves as if the table/view is empty.
I decided to track the issue down manually and used the shell_plus command to open an interactive python shell. In there, I’ve executed the following commands:
>>> from django.db import connections
>>> cursor = connections["secondary"].cursor()
>>> cursor.execute("select COUNT(*) from my_view")
<django.db.backends.postgresql.base.Cursor [TUPLES_OK] [IDLE] (host=my.db user=my_user database=secondary) at 0x7fbc271d7c50>
>>> cursor.fetchone()
(0,)
>>>
One the other hand, trying to use the dbshell directly works without any problems:
$ python3 manage.py dbshell --database=secondary
secondary=> select COUNT(*) from my_view;
count
-------
2595
(1 row)
At this point I don’t really know how to proceed as the issue does not seem to be an issue with routing, syntax or permissions. Any help is highly appreciated!