简体   繁体   English

Django:用于 prefetch_related 的数据库与父查询不同

[英]Django: Database used for prefetch_related is not the same that the parent query

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.我在我的 Django 应用程序中使用了一个多数据库和一个主数据库和一个只读副本,但为了避免复制滞后问题,路由器总是使用默认数据库,除了我手动设置数据库的少数地方。

I'm facing an issue as I don't know how to specify the database used for prefetch_related.我面临一个问题,因为我不知道如何指定用于 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.例如,我希望下一个查询仅使用read_replica DB,但它执行 2 个查询,第一个按预期转到read_replica ,第二个转到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;选择@@SQL_AUTO_IS_NULL; args=None参数=无

SELECT VERSION();选择版本(); args=None参数=无

SELECT ... FROM ftmanager_userprofile WHERE ( ftmanager_userprofile . id IN (33); args=(33,) SELECT ... FROM ftmanager_userprofile WHERE( ftmanager_userprofileid IN(33); ARGS =(33,)

SELECT @@SQL_AUTO_IS_NULL;选择@@SQL_AUTO_IS_NULL; args=None参数=无

SELECT VERSION();选择版本(); args=None参数=无

SELECT ... FROM ftmanager_usermedia WHERE ftmanager_usermedia . SELECT ... FROM ftmanager_usermedia WHERE ftmanager_usermedia user_id IN (33); user_id输入 (33); args=(33,)参数=(33,)

I see a related ticket on Django tickets but I do not understand how to apply using() to inner queryset.我在Django票证上看到了相关票证,但我不明白如何将using()应用于内部查询集。

I have found the solution thinking about the Django ticket, you need to use the Prefetch class to define the inner queryset used by prefetch_related, it really messes the code but the gain deserves it:我找到了考虑 Django 票证的解决方案,您需要使用Prefetch类来定义 prefetch_related 使用的内部查询集,它确实弄乱了代码,但收益值得:

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)

I've encountered the same problem.我遇到了同样的问题。 Configuring the router (namely, db_for_read and using instance from hints) helped:配置路由器(即 db_for_read 并使用提示中的实例)有助于:

def db_for_read(self, model, **hints):
    instance = hints.get('instance')
    if instance is not None:
        if instance._meta.app_label in self.route_app_labels:
            return self.route_db_name 
    elif model._meta.app_label in self.route_app_labels:
        return self.route_db_name 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM