[autotest] Fix a bug in TKO query with custom fields.

BUG=chromium:441017
TEST=local check, unittest

Change-Id: Ia379411d7714f0b5e4318efad4ac1ec1681f957b
Reviewed-on: https://chromium-review.googlesource.com/437544
Commit-Ready: Dan Shi <dshi@google.com>
Tested-by: Dan Shi <dshi@google.com>
Reviewed-by: Richard Barnette <jrbarnette@google.com>
diff --git a/frontend/afe/model_logic.py b/frontend/afe/model_logic.py
index 580e7cd..5fe544b 100644
--- a/frontend/afe/model_logic.py
+++ b/frontend/afe/model_logic.py
@@ -169,7 +169,10 @@
         rhs_where.relabel_aliases({rhs_table: alias})
         compiler = join_to_query.query.get_compiler(using=join_to_query.db)
         initial_clause, values = compiler.as_sql()
-        all_clauses = (initial_clause,)
+        # initial_clause is compiled from `join_to_query`, which is a SELECT
+        # query returns at most one record. For it to be used in WHERE clause,
+        # it must be converted to a boolean value using EXISTS.
+        all_clauses = ('EXISTS (%s)' % initial_clause,)
         if hasattr(join_to_query.query, 'extra_where'):
             all_clauses += join_to_query.query.extra_where
         info['where_clause'] = (