def test_subquery(self):
        sub = Person.objects.using('other').filter(name='fff')
        qs = Book.objects.filter(editor__in=sub)

        # When you call __str__ on the query object, it doesn't know about using
        # so it falls back to the default. If the subquery explicitly uses a
        # different database, an error should be raised.
        with self.assertRaises(ValueError):
            str(qs.query)

        # Evaluating the query shouldn't work, either
        with self.assertRaises(ValueError):
            for obj in qs:
                pass
Make sure as_sql works with subqueries and primary/replica.
dataset/ETHPy150Open django/django/tests/multiple_database/tests.py/QueryTestCase.test_subquery