tortoise / tortoise/tortoise-orm
Columns named `pk` cause RecursionError
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
from tortoise import Tortoise, run_async, fields
from tortoise.models import Model
class TestModel(Model):
pk = fields.IntField(pk=True)
async def main():
await Tortoise.init(
db_url="sqlite://memory", modules={"models": ["__main__"]}
)
await Tortoise.generate_schemas()
await TestModel.create(pk=0)
run_async(main())
This errors with the following
Traceback (most recent call last):
File "/Users/donjar/hack/good_tortoise.py", line 17, in <module>
run_async(main())
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/__init__.py", line 688, in run_async
loop.run_until_complete(coro)
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/Users/donjar/hack/good_tortoise.py", line 14, in main
await TestModel.create(pk=0)
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/models.py", line 1133, in create
instance = cls(**kwargs)
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/models.py", line 672, in __init__
for key in meta.fields.difference(self._set_kwargs(kwargs)):
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/models.py", line 699, in _set_kwargs
setattr(self, key, field_object.to_python_value(value))
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/models.py", line 776, in _set_pk_val
setattr(self, self._meta.pk_attr, value)
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/models.py", line 776, in _set_pk_val
setattr(self, self._meta.pk_attr, value)
File "/Users/donjar/.pyenv/versions/3.9.14/lib/python3.9/site-packages/tortoise/models.py", line 776, in _set_pk_val
setattr(self, self._meta.pk_attr, value)
[Previous line repeated 490 more times]
RecursionError: maximum recursion depth exceeded
Using other column names work, as is using this:
class TestModel(Model):
pk2 = fields.IntField(pk=True, source_field="pk")
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in tortoise/models.py at Model.init, _set_kwargs, and _set_pk_val, following the recursion shown when a field is named pk. Reproduce the issue with the provided SQLite example and verify that creating TestModel with pk=0 completes without RecursionError while preserving the primary-key behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100