tortoise / tortoise/tortoise-orm

pydantic_model_creator: different model has the same var "_name" will be replace in global _MODEL_INDEX

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

Describe the bug
file: tortoise/contrib/pydantic/creator.py Line: 392

# Here we endure that the name is unique, but complete objects are still labeled verbatim
if not has_submodel:
    _name = name or f"{fqname}.leaf"
elif has_submodel:
    _name = name or get_name()

This Code Will Have a global impact.

To Reproduce
class Child(BaseModel):
id: str
name: str
childs_1: fields.ReverseRelation["P1"]
childs_2: fields.ReverseRelation["P2"]

class P1(BaseModel):
id: str
name: str
child: fields.ForeignKeyRelation["Child"] = fields.ForeignKeyField(
"Child", related_name="childs_1")

class P2(BaseModel):
id: str
name: str
child: fields.ForeignKeyRelation["Child"] = fields.ForeignKeyField(
"Child", related_name="childs_2")

P1_Entity = pydantic_model_creator(P1, name="P1", exclude=("child.id"))
P2_Entity = pydantic_model_creator(P2, name="P2")

And Then:
P2_Entity.child has not property: id

Expected behavior
P2_Entity.child has property: id

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 in tortoise/contrib/pydantic/creator.py around line 392 and reproduce the example with Child, P1, and P2. Inspect how generated names are stored in the global model index and verify that excluding child.id from P1 does not remove id from P2_Entity.child. Done means the expected properties are restored for both generated models.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.