tortoise / tortoise/tortoise-orm

max recursion bug

Open
#1,627 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

https://github.com/tortoise/tortoise-orm/blob/7160e75a36b9f07be2d99551838bdb323d0b142b/tortoise/contrib/pydantic/creator.py#L325

I suppose this logic incorrect, simple example:

class DBBaseModel(Model):
    class PydanticMeta:
        backward_relations = True
        allow_cycles = False
        max_recursion = 3
        exclude_raw_fields = False
# A->B->C->D->E
class A(DBBaseModel):
   ...
   B: ReverseRelation["B"]

class B(DBBaseModel):
   C: ReverseRelation["C"]
   ...

class C(DBBaseModel):
  D: ReverseRelation["D"]
   ...

class D(DBBaseModel):
  E: ReverseRelation["E"]
   ...

class E(DBBaseModel):
   ...
   
class MetaOvveride(PydanticMeta):
    backward_relations = False
    allow_cycles = False
    max_recursion = 0
    exclude_raw_fields = False

pydantic_a = pydantic_model_creator(A, meta_override=MetaOvveride)

result = await pydantic_a.from_queryset(A.filter(...))

Here I get all data from all relationship, but max_recursion is 0, this part of code doesn't work, also if max_recursion = 1 this part returns all models from relationships, but when max_recursion = 1 we must get A and B, I think we must watch max_recursion only in A model not in others, and changing max_recursion by -1 when we do a recursive traversal

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

Start at tortoise/contrib/pydantic/creator.py around line 325 and reproduce the A→B→C→D→E example using pydantic_model_creator. Trace how max_recursion and backward_relations are applied during recursive traversal. Done means max_recursion=0 prevents relationship expansion and max_recursion=1 includes only the intended first relationship level.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.