问题
I'm using a multi-db in my Django app with a master database and a read replica, but to avoid replication lag problems the router always uses default database except few places where I set DB manually.
I'm facing an issue as I don't know how to specify the database used for prefetch_related.
For example I want the next query to use only read_replica
DB, but it does 2 queries, the first goes to read_replica
as expected and the second goes to default
DB.
users = UserProfile.objects.using('read_replica').prefetch_related('usermedia_set').filter(id__in=user_ids)
This are the output of this query:
SELECT @@SQL_AUTO_IS_NULL; args=None
SELECT VERSION(); args=None
SELECT ... FROM
ftmanager_userprofile
WHERE (ftmanager_userprofile
.id
IN (33); args=(33,)SELECT @@SQL_AUTO_IS_NULL; args=None
SELECT VERSION(); args=None
SELECT ... FROM
ftmanager_usermedia
WHEREftmanager_usermedia
.user_id
IN (33); args=(33,)
I see a related ticket on Django tickets but I do not understand how to apply using()
to inner queryset.
回答1:
I have found the solution thinking about the Django ticket, you need to use thePrefetch
class to define the inner queryset used by prefetch_related, it really messes the code but the gain deserves it:
from django.db.models.query import Prefetch
users = UserProfile.objects.using('read_replica').prefetch_related(Prefetch('usermedia_set', queryset=UserMedia.objects.using('read_replica'))).filter(id__in=user_ids)
来源:https://stackoverflow.com/questions/40139674/django-database-used-for-prefetch-related-is-not-the-same-that-the-parent-query