exec ( statement ) for hero, team in results : print ( "Hero:", hero, "Team:", team ) def main (): create_db_and_tables () create_heroes () select_heroes () if _name_ = "_main_" : main ()įor each iteration in the for loop we get a a tuple with an instance of the class Hero and an instance of the class Team.Īnd in this for loop we assign them to the variable hero and the variable team. refresh ( hero_spider_boy ) print ( "Created hero:", hero_deadpond ) print ( "Created hero:", hero_rusty_man ) print ( "Created hero:", hero_spider_boy ) def select_heroes (): with Session ( engine ) as session : statement = select ( Hero, Team ). id, ) hero_spider_boy = Hero ( name = "Spider-Boy", secret_name = "Pedro Parqueador" ) session. The sqlite3openv2 () interface works like sqlite3open () except that it accepts two additional parameters for additional control over the new database connection. id ) hero_rusty_man = Hero ( name = "Rusty-Man", secret_name = "Tommy Sharp", age = 48, team_id = team_preventers. commit () hero_deadpond = Hero ( name = "Deadpond", secret_name = "Dive Wilson", team_id = team_z_force. create_all ( engine ) def create_heroes (): with Session ( engine ) as session : team_preventers = Team ( name = "Preventers", headquarters = "Sharp Tower" ) team_z_force = Team ( name = "Z-Force", headquarters = "Sister Margaret’s Bar" ) session. Test Applications with FastAPI and SQLModelĪlternatives, Inspiration and Comparisonsįrom typing import Optional from sqlmodel import Field, Session, SQLModel, create_engine class Team ( SQLModel, table = True ): id : Optional = Field ( default = None, primary_key = True ) name : str = Field ( index = True ) headquarters : str class Hero ( SQLModel, table = True ): id : Optional = Field ( default = None, primary_key = True ) name : str = Field ( index = True ) secret_name : str age : Optional = Field ( default = None, index = True ) team_id : Optional = Field ( default = None, foreign_key = "team.id" ) sqlite_file_name = "database.db" sqlite_url = f "sqlite:/// " engine = create_engine ( sqlite_url, echo = True ) def create_db_and_tables (): SQLModel. Read Heroes with Limit and Offset with FastAPIįastAPI Path Operations for Teams - Other Models Update and Remove Many-to-Many Relationships JOIN Tables with SQL and LEFT OUTER (Maybe JOIN)Ĭreate Data with Many-to-Many Relationships Create a Table with SQLModel - Use the EngineĪutomatic IDs, None Defaults, and Refreshing Data
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |