# Buggy code
def __init__(
    self,
    model: "Type[Model]",
    db: "BaseDBAsyncClient",
    prefetch_map=None,
    prefetch_queries=None,
) -> None:
    self.model = model
    self.db: "BaseDBAsyncClient" = db
    self.prefetch_map = prefetch_map if prefetch_map else {}
    self._prefetch_queries = prefetch_queries if prefetch_queries else {}

    key = f"{self.db.connection_name}:{self.model._meta.table}"
    if key not in EXECUTOR_CACHE:
        self.regular_columns, columns = self._prepare_insert_columns()
        self.insert_query = self._prepare_insert_statement(columns)

        self.column_map: Dict[str, Callable[[Any, Any], Any]] = {}
        for column in self.regular_columns:
            field_object = self.model._meta.fields_map[column]
            if field_object.__class__ in self.TO_DB_OVERRIDE:
                self.column_map[column] = partial(
                    self.TO_DB_OVERRIDE[field_object.__class__], field_object
                )
            else:
                self.column_map[column] = field_object.to_db_value

        table = Table(self.model._meta.table)
        self.delete_query = str(
            self.model._meta.basequery.where(
                getattr(table, self.model._meta.db_pk_field) == self.Parameter(0)
            ).delete()
        )
        self.update_cache: Dict[str, str] = {}

        EXECUTOR_CACHE[key] = (
            self.regular_columns,
            self.insert_query,
            self.column_map,
            self.delete_query,
            self.update_cache,
        )
    else:
        (
            self.regular_columns,
            self.insert_query,
            self.column_map,
            self.delete_query,
            self.update_cache,
        ) = EXECUTOR_CACHE[key]


# Traceback (AttributeError)
Traceback (most recent call last):
File "/opt/project/src/server.py", line 54, in <module>
asyncio.run(main())
File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
return future.result()
File "/opt/project/src/server.py", line 47, in main
async for item in ModelOne.all().prefetch_related('many_to_many'):
File "/usr/local/lib/python3.7/site-packages/tortoise/queryset.py", line 512, in __aiter__
for val in await self:
File "/usr/local/lib/python3.7/site-packages/tortoise/queryset.py", line 521, in _execute
).execute_select(self.query, custom_fields=list(self._annotations.keys()))
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 90, in execute_select
await self._execute_prefetch_queries(instance_list)
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 327, in _execute_prefetch_queries
await self._do_prefetch(instance_list, field, related_query)
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 320, in _do_prefetch
return await self._prefetch_m2m_relation(instance_id_list, field, related_query)
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 232, in _prefetch_m2m_relation
*[getattr(related_query_table, field).as_(field) for field in related_query.fields],
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 232, in <listcomp>
*[getattr(related_query_table, field).as_(field) for field in related_query.fields],
AttributeError: 'NoneType' object has no attribute 'as_'

# Fixed code
def __init__(
    self,
    model: "Type[Model]",
    db: "BaseDBAsyncClient",
    prefetch_map=None,
    prefetch_queries=None,
) -> None:
    self.model = model
    self.db: "BaseDBAsyncClient" = db
    self.prefetch_map = prefetch_map if prefetch_map else {}
    self._prefetch_queries = prefetch_queries if prefetch_queries else {}

    key = f"{self.db.connection_name}:{self.model._meta.table}"
    if key not in EXECUTOR_CACHE:
        self.regular_columns, columns = self._prepare_insert_columns()
        self.insert_query = self._prepare_insert_statement(columns)

        self.column_map: Dict[str, Callable[[Any, Any], Any]] = {}
        for column in self.regular_columns:
            field_object = self.model._meta.fields_map[column]
            if field_object.__class__ in self.TO_DB_OVERRIDE:
                self.column_map[column] = partial(
                    self.TO_DB_OVERRIDE[field_object.__class__], field_object
                )
            else:
                self.column_map[column] = field_object.to_db_value

        table = Table(self.model._meta.table)
        self.delete_query = str(
            self.model._meta.basequery.where(
                table[self.model._meta.db_pk_field] == self.Parameter(0)
            ).delete()
        )
        self.update_cache: Dict[str, str] = {}

        EXECUTOR_CACHE[key] = (
            self.regular_columns,
            self.insert_query,
            self.column_map,
            self.delete_query,
            self.update_cache,
        )
    else:
        (
            self.regular_columns,
            self.insert_query,
            self.column_map,
            self.delete_query,
            self.update_cache,
        ) = EXECUTOR_CACHE[key]