tortoise / tortoise-orm

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

max recursion bug #1627

Open olegkorshunov opened 1 month ago

olegkorshunov commented 1 month ago

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