fastapi database code example
Example 1: fastapi
pip install fastapi
pip install uvicorn
pip install starlette
pip install pydantic
pip install fastapi uvicorn starlette pydantic
Example 2: insert into relational database fastapi
def create_nested_family(db: Session, family: schemas.NestedFamilyCreate):
family_data = family.dict()
member_data = family_data.pop('members', None)
db_family = models.Family(**family_data)
db.add(db_family)
db.commit()
db.refresh(db_family)
family_id = db_family.id
for m in member_data:
m['family_id'] = family_id
db_member = models.Member(**m)
db.add(db_member)
db.commit()
db.refresh(db_member)
return db_family