# Buggy code
def get_update_sql(self, update_fields: Optional[List[str]]) -> str:
    """
    Generates the SQL for updating a model depending on provided update_fields.
    Result is cached for performance.
    """
    key = ",".join(update_fields) if update_fields else ""
    if key in self.update_cache:
        return self.update_cache[key]

    table = Table(self.model._meta.table)
    query = self.db.query_class.update(table)
    count = 0
    for field in update_fields or self.model._meta.fields_db_projection.keys():
        db_field = self.model._meta.fields_db_projection[field]
        field_object = self.model._meta.fields_map[field]
        if not field_object.pk:
            query = query.set(db_field, self.Parameter(count))
            count += 1

    query = query.where(
        getattr(table, self.model._meta.db_pk_field) == self.Parameter(count)
    )

    sql = self.update_cache[key] = query.get_sql()
    return sql


# 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 get_update_sql(self, update_fields: Optional[List[str]]) -> str:
    """
    Generates the SQL for updating a model depending on provided update_fields.
    Result is cached for performance.
    """
    key = ",".join(update_fields) if update_fields else ""
    if key in self.update_cache:
        return self.update_cache[key]

    table = Table(self.model._meta.table)
    query = self.db.query_class.update(table)
    count = 0
    for field in update_fields or self.model._meta.fields_db_projection.keys():
        db_field = self.model._meta.fields_db_projection[field]
        field_object = self.model._meta.fields_map[field]
        if not field_object.pk:
            query = query.set(db_field, self.Parameter(count))
            count += 1

    query = query.where(table[self.model._meta.db_pk_field] == self.Parameter(count))

    sql = self.update_cache[key] = query.get_sql()
    return sql