Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in pydantic_model_creator when include does not contain foreing keys. #1683

Merged
merged 1 commit into from
Aug 5, 2024

Conversation

Abdeldjalil-H
Copy link
Contributor

When the function pydantic_model_creator is used to create a model and the parameter include is passed #1430, this will raise an error if the relation is passed but not the foreign key value. Here is an example:

class Tournament(Model):
    """
    This references a Tournament
    """

    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=100)


class Event(Model):
    """
    This references an Event in a Tournament
    """

    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=100)

    tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField(
        "models.Tournament", related_name="events", description="The Tournament this happens in"
    )

# Initialise model structure early.
Tortoise.init_models(["__main__"], "models")

Event_Pydantic = pydantic_model_creator(Event, include=["tournament"])

The above code will raise a KeyError exception because tournament is not included, thus it will not be found in field_map variable of the function field_map_update. To fix this we can replace the del with pop method.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 10132332526

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 89.158%

Totals Coverage Status
Change from base Build 10115140001: 0.0%
Covered Lines: 5939
Relevant Lines: 6550

💛 - Coveralls

@abondar abondar merged commit 5ca0c5f into tortoise:develop Aug 5, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants