tortoise / tortoise/tortoise-orm

Frequent use of PyDantic_Model_Creator results in subschema field exceptions

Open
#775 1 comment 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
In pydantic_model_creator ,the program automatically generate submodel's name.
When I use it frequently,every submodel will influence each other.

To Reproduce
model:

class Author(Model):
    name = fields.CharField(max_length=255)


class Book(Model):
    name = fields.CharField(max_length=255)
    author = fields.ForeignKeyField("fast_tmp.Author", related_name="books")
    rating = fields.FloatField()

schema:

# 这里主要保存根据model生成的schema
from pydantic import BaseModel
from tortoise.contrib.pydantic import pydantic_model_creator
from .models import Author, Book

x1 = pydantic_model_creator(Book, name="x1", include=(
"name", "author_id", "author", "author.id"))
x2 = pydantic_model_creator(Book, name="x2", include=(
"name", "author_id", "author", "author.id", "author.name"))
a1=x1.schema()
a2=x2.schema()
# a1 = {'title': 'x1', 'type': 'object', 'properties': {
#     'name': {'title': 'Name', 'maxLength': 255, 'type': 'string'},
#     'author': {'title': 'Author', 'allOf': [
#         {'$ref': '#/definitions/test_example.models.Author.leaf'}]}},
#       'required': ['name', 'author'], 'additionalProperties': False,
#       'definitions': {'test_example.models.Author.leaf': {'title': 'Author',
#                                                           'type': 'object',
#                                                           'properties': {'id': {
#                                                               'title': 'Id',
#                                                               'minimum': 1,
#                                                               'maximum': 2147483647,
#                                                               'type': 'integer'}},
#                                                           'required': ['id'],
#                                                           'additionalProperties': False}}}
# a2 = {'title': 'x2', 'type': 'object', 'properties': {
#     'name': {'title': 'Name', 'maxLength': 255, 'type': 'string'},
#     'author': {'title': 'Author', 'allOf': [
#         {'$ref': '#/definitions/test_example.models.Author.leaf'}]}},
#       'required': ['name', 'author'], 'additionalProperties': False,
#       'definitions': {'test_example.models.Author.leaf': {'title': 'Author',
#                                                           'type': 'object',
#                                                           'properties': {'id': {
#                                                               'title': 'Id',
#                                                               'minimum': 1,
#                                                               'maximum': 2147483647,
#                                                               'type': 'integer'}},
#                                                           'required': ['id'],
#                                                           'additionalProperties': False}}}

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.pydantic_model_creator and reproduce the x1/x2 sequence from the issue, comparing their generated schemas and subschema definitions. Done means repeated calls no longer cause subschema field exceptions and the differing include paths behave independently.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, 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.