First Check
Commit to Help
Example Code
fromtypingimportList, OptionalfromsqlmodelimportField, Relationship, Session, SQLModel, create_engineclassTeam(SQLModel, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
name: str=Field(index=True)
headquarters: strheroes: List["Hero"] =Relationship(back_populates="team")
classHero(SQLModel, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
name: str=Field(index=True)
secret_name: strage: Optional[int] =Field(default=None, index=True)
team_id: Optional[int] =Field(default=None, foreign_key="team.id")
team: Optional[Team] =Relationship(back_populates="heroes")
sqlite_file_name="database.db"sqlite_url=f"sqlite:///{sqlite_file_name}"engine=create_engine(sqlite_url, echo=True)
defcreate_db_and_tables():
SQLModel.metadata.create_all(engine)
defcreate_heroes():
withSession(engine) assession:
team_preventers=Team(name="Preventers", headquarters="Sharp Tower")
team_z_force=Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
hero_deadpond=Hero(
name="Deadpond", secret_name="Dive Wilson", team=team_z_force
)
hero_rusty_man=Hero(
name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers
)
hero_spider_boy=Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
session.add(hero_deadpond)
session.add(hero_rusty_man)
session.add(hero_spider_boy)
session.commit()
session.refresh(hero_deadpond)
session.refresh(hero_rusty_man)
session.refresh(hero_spider_boy)
print("Created hero:", hero_deadpond)
print("Created hero:", hero_rusty_man)
print("Created hero:", hero_spider_boy)
hero_spider_boy.team=team_preventerssession.add(hero_spider_boy)
session.commit()
defmain():
create_db_and_tables()
create_heroes()
if__name__=="__main__":
main()Description
Follow the tutorial chapter: https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/create-and-update-relationships/#create-instances-with-relationship-attributes
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.9.0
Additional Context
It is not possible to assign a new hero to a new team with relationship attributes (see https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/create-and-update-relationships/#create-instances-with-relationship-attributes). The provided code (https://github.com/tiangolo/sqlmodel/blob/main/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py) does not work as expected.
First Check
Commit to Help
Example Code
Description
Follow the tutorial chapter: https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/create-and-update-relationships/#create-instances-with-relationship-attributes
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.9.0
Additional Context
It is not possible to assign a new hero to a new team with relationship attributes (see https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/create-and-update-relationships/#create-instances-with-relationship-attributes). The provided code (https://github.com/tiangolo/sqlmodel/blob/main/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py) does not work as expected.