From 62f01edee364543a995bdced06328643ba7bd7bb Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Wed, 12 Jul 2023 15:31:22 +0530 Subject: [PATCH] populate_db: Pass realm as arg to select_related calls. This commit updates the select_related calls in queries to get UserProfile objects in populate_db code to pass "realm" as argument to select_related call. Also, note that "realm" is the only non-null foreign key field in UserProfile object, so select_related() was only fetching realm object previously as well. But we should still pass "realm" as argument in select_related call so that we can make sure that only required fields are selected in case we add more foreign keys to UserProfile in future. --- zilencer/management/commands/populate_db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 8f1fd3512c..a625416df5 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -132,7 +132,7 @@ def subscribe_users_to_streams(realm: Realm, stream_dict: Dict[str, Dict[str, An subscriptions_to_add = [] event_time = timezone_now() all_subscription_logs = [] - profiles = UserProfile.objects.select_related().filter(realm=realm) + profiles = UserProfile.objects.select_related("realm").filter(realm=realm) for i, stream_name in enumerate(stream_dict): stream = Stream.objects.get(name=stream_name, realm=realm) recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id) @@ -621,7 +621,7 @@ class Command(BaseCommand): subscriptions_list: List[Tuple[UserProfile, Recipient]] = [] profiles: Sequence[UserProfile] = list( - UserProfile.objects.select_related().filter(is_bot=False).order_by("email") + UserProfile.objects.select_related("realm").filter(is_bot=False).order_by("email") ) if options["test_suite"]: