From 99908c6a5a4995c75379a1d81587a47304b79dfc Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Mon, 22 May 2023 08:28:00 -0500 Subject: [PATCH] add pet --- learn_sql_model/factories/pet.py | 16 ++++++++++++++++ learn_sql_model/models/pet.py | 14 ++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 learn_sql_model/factories/pet.py diff --git a/learn_sql_model/factories/pet.py b/learn_sql_model/factories/pet.py new file mode 100644 index 0000000..681dac1 --- /dev/null +++ b/learn_sql_model/factories/pet.py @@ -0,0 +1,16 @@ +from polyfactory.factories.pydantic_factory import ModelFactory + +from learn_sql_model.models.hero import Hero +from learn_sql_model.models.pet import Pet + +__relationship__ = [Hero] + + +class PetFactory(ModelFactory[Pet]): + __model__ = Pet + __set_as_default_factory_for_type__ = True + id = None + + @classmethod + def name(cls) -> str: + return cls.__faker__.first_name() diff --git a/learn_sql_model/models/pet.py b/learn_sql_model/models/pet.py index bc167e3..a9b4314 100644 --- a/learn_sql_model/models/pet.py +++ b/learn_sql_model/models/pet.py @@ -1,16 +1,14 @@ -from typing import Optional +from typing import Optional, TYPE_CHECKING -from sqlmodel import Field +from sqlmodel import Field, Relationship from learn_sql_model.models.fast_model import FastModel +if TYPE_CHECKING: + from learn_sql_model.models.hero import Hero + class Pet(FastModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str = "Jim" - - -# age: Optional[int] = None - -# hero_id: int = Field(default=None, foreign_key="hero.id") -# hero: Optional[Hero] = Relationship(back_populates="pets") + hero: "Hero" = Relationship(back_populates="pet")