tortoise / tortoise/tortoise-orm

Recursion error

Open
#1,411 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Recursion error occurs when I try to write data to the database

tortoise config
DB_CONFIG = { 'connections': { # Dict format for connection 'default': { 'engine': 'tortoise.backends.asyncpg', 'credentials': { 'host': 'db', 'port': '5432', 'user': 'my_user', 'password': 'mysecretpassword', 'database': 'postgres', } }, }, 'apps': { 'models': { 'models': ["aerich.models", 'models.cargo', 'models.rate'], # If no default_connection specified, defaults to 'default' 'default_connection': 'default', } } }
tortoise connection in main.py
register_tortoise( app=app, config=DB_CONFIG, generate_schemas=True, add_exception_handlers=True )

tortoise model
class Cargo(models.Model): pk = fields.IntField(pk=True) name = fields.CharField(max_length=30)
pydantic model
class Cargo(BaseModel): pk: int name: str

endpoint
@router.post('/') async def post(cargo: pydantic.Cargo): return await Cargo.create(pk=cargo.pk, name=cargo.name)
stack trace
File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 1136, in create instance = cls(**kwargs) File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 671, in __init__ for key in meta.fields.difference(self._set_kwargs(kwargs)): File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 698, in _set_kwargs setattr(self, key, field_object.to_python_value(value)) File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 775, in _set_pk_val setattr(self, self._meta.pk_attr, value) File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 775, in _set_pk_val setattr(self, self._meta.pk_attr, value) File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 775, in _set_pk_val setattr(self, self._meta.pk_attr, value) [Previous line repeated 480 more times] RecursionError: maximum recursion depth exceeded

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the endpoint using the shown Tortoise configuration and models, then inspect tortoise/models.py around lines 671, 698, and 775 where model initialization and primary-key assignment recurse. Done means the reported write no longer raises RecursionError and the Cargo record is created successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.