tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.51k stars 369 forks source link

Nested select_related raises KeyError inside _init_from_db #1396

Open Tauassar opened 1 year ago

Tauassar commented 1 year ago

Describe the bug When using nested select_related of form: 'modelA__modelB' I'm getting a KeyError when ORM tries to assign values to modelB.

An issue arises since kwargs values passed to _init_from_db function getting sliced version of model attribute's name, for example, instead of passing 'city_obj' attribute, 'city_ob' is being passed to kwargs, same way instead of attribute 'building', 'buildin' is being passed etc.

Actual error:

File "/usr/local/lib/python3.9/site-packages/tortoise/queryset.py", line 1008, in _execute instance_list = await self._db.executor_class( File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 155, in execute_select obj = model._init_from_db( File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 747, in _init_from_db setattr(self, key, meta.fields_map[key].to_python_value(value)) KeyError: 'buildin'

ORM version: tortoise-orm==0.19.3

jar3b commented 5 months ago

Same issue with 0.20.0